Java concurrency 1-synchronization between threads

Source: Internet
Author: User

The common point of thread synchronization is two points:
1. When a thread processes data, it can notify other threads that they have finished their jobs, and other threads can use the data it processes to do other things. (Object. Policy, object. policyall)
2. When a thread needs the computing results of other threads, it needs to wait until it knows that other threads have finished the job and it gets the data to start its own job. (Object. Wait)

Java fully supports concurrency and provides support directly in the base class object. The three most common methods are listed as follows (as mentioned above ):
1. Object. Y: Notify a thread to work properly. You can start the job. A thread that calls the wait method of this object stops blocking and returns.
2. Object. policyall: notifies all threads of active tasks. All threads that call the wait method of this object stop blocking and return.
3. Object. Wait: Wait until other threads have finished working and start the job on their own. This method will be blocked until a thread calls policy or policyall.

 

The following is a simple example. The example is intended to work with three threads A, B, and C. Each process requires a to finish first, then B to start, and B to start C, and then go to the next step.
Flowchart:

Java code

1   /**   2   * Concurrent printing  3   *  @ Author  Shun. Li  4        */  5       Static   Class Concurrentprinter Implements  Runnable  6   { 7           8           //  Thread mutex variable  9           Private   Static Object mutex = New  Object ();  10           //  Name of the thread currently qualified for Printing  11           Private   Static String currentname = "" ; 12           Public   Static   Void  Setcurrentname (string name ){  13 Currentname = Name;  14   }  15           16           Private String name; //  Thread name  17           Private String nextname; //  Name of the next printable thread  18           Private   Int Count; //  Print count  19           20           Public Concurrentprinter (string name, string nextname, Int  Count ){  21               This . Name = Name; 22               This . Nextname = Nextname;  23               This . Count = Count;  24   }  25           26           /**  27   * Print information. The number of prints is  28            */  29          Public   Void  Print (){  30               //  : Printing mutex. Only one printing thread can start after printing.  31               Synchronized  (Mutex ){  32                   33                   //  : Wait for the previous thread to notify you to print it.  34                   While (Currentname! = Name ){ 35                       Try  {  36   Mutex. Wait ();  37 } Catch  (Interruptedexception e ){  38                           Return  ;  39   }  40   } 41                   //  : Print  42                   For ( Int I = 0; I <count; ++ I ){  43 System. Out. println (string. Format ("% 1 $ 04d: % 2 $ S" , I, name ));  44   }  45                   //  Set the executable thread name to the next  46 Currentname = Nextname;  47                   //  Notifies all threads that the thread named currentname meets the conditions and starts printing.  48   Mutex. policyall ();  49   }  50   }  51   52   @ Override  53           Public  Void  Run (){  54               //  Each thread executes a printing task for 100 times.  55               For ( Int I = 0; I <10; ++ I ){  56                   This  . Print ();  57   }  58   } 59   }  60   61       /**  62   *  @ Param  ARGs  63   *  @ Throws  Ioexception  64        */  65       Public  Static   Void  Main (string [] ARGs ){  66 System. Out. println ("alala" );  67               68               //  : Create three threads and print them in turn  69 Thread athread = New Thread ( New Concurrentprinter ("A", "B", 3 ));  70 Thread bthread =New Thread ( New Concurrentprinter ("B", "C", 3 ));  71 Thread cthread = New Thread ( New Concurrentprinter ("C", "A", 3 ));  72 Concurrentprinter. setcurrentname ("" );  73   Athread. Start ();  74   Bthread. Start (); 75   Cthread. Start ();  76 }

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.