Java Concurrency Implementation One (the difference between the thread and runnable of the concurrency implementation)

Source: Internet
Author: User
Tags ticket

Package Com.subject01;public class threadorrunnable  {public static void main (string[] args) {System.out.println (" Thread output: ");//Three threads for three different object resources, resources cannot be shared. Because it is not the same object, there is no thread-safe problem with new Threaddemo (). Start (); New Threaddemo (). Start (); New Threaddemo (). Start (); SYSTEM.OUT.PRINTLN ("Runnable Interface Implementation output:");//Three threads use the same object resource together to achieve resource sharing. However, there is also a thread insecurity problem, so object resources need to add synchronization settings Runnabledemo Rundemo = new Runnabledemo (); new Thread (Rundemo). Start (); New Thread ( Rundemo). Start (); new Thread (Rundemo). Start ();}} Class Threaddemo extends thread{private int ticket = 5; @Overridepublic void Run () {while (ticket>0) {System.out.println ("ticket=" +ticket--);}}}  Class Runnabledemo implements runnable{private int ticket = 5; @Overridepublic void Run () {//thread insecure, need to add synchronization settings synchronized (this) {while (ticket>0) {try {thread.sleep (1000L);} catch (Interruptedexception e) {e.printstacktrace ();} System.err.println ("ticket=" +ticket--);}}}

http://blog.csdn.net/ns_code/article/details/17161237

Java Concurrency Implementation One (the difference between the thread and runnable of the concurrency implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.