Part 6 auxiliary Thread Synchronization

Source: Internet
Author: User
Auxiliary thread synchronization class

Cyclicbarrier

Java. uril. concurrent. cyclicbarrier is a synchronization helper class, but the synchronization mentioned here is not the "thread synchronization" that has been emphasized earlier in this chapter ", it means that the threads in the same thread group must arrive at the specified "Consortium. This kind of synchronization is similar to the field training of soldiers. Instructors require that they must gather in several locations and then continue to split their heads, and then gather in a certain location... these locations are called "Gathering points"

The constructor of the javasicbarrier class requires the number of threads required for synchronization. The most important method of the javasicbarrier class is the await () method. It is through this method that the thread waits.

As you can see in the previous chapters, the Java. util. Concurrent. executor interface and its sub-interface implementation classes represent the thread pool. Java. util. concurrent. the executorservice interface inherits from Java. util. concurrent. executor interface, in addition to using the execute () method of the executor interface to submit the task to the thread pool for immediate execution, this article will also contact a method defined by the executorservice interface submit (runnable ), used to submit tasks in the thread pool.

The following code demonstrates how to synchronize the three threads in the auxiliary thread pool at the three "meeting points": the first line of the thread has the shortest sleep time, so it is always the first to reach the meeting point, it needs to wait for the other two threads.

Code [testcyclicbarrier]

/** <Br/> * testpolicicbarrier. java <br/> * copyright (c) 2011 cuiran2001@163.com <br/> * Create: cui ran 2011-1-14 02:15:11 PM <br/> */</P> <p> package COM. cayden. thread832; </P> <p> Import Java. util. date; <br/> Import Java. util. concurrent. brokenbarrierexception; <br/> Import Java. util. concurrent. cyclicbarrier; <br/> Import Java. util. concurrent. executorservice; <br/> Import Java. util. concurrent. executors; </P> <p>/** <br/> * @ author Cui ran <br/> * @ version 1.0.0 <br/> * @ DESC thread synchronization auxiliary class learning <br /> */<br/> public class testcyclicbarrier {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> // create a synchronization helper object that needs to be synchronized to three threads <br/> javasicbarrier barrier = new javasicbarrier (3 ); <br/> // create a fixed thread pool with a capacity of 3, wait <br/> executorservice exec = executors. newfixedthreadpool (3); </P> <p> // submit three tasks to the thread pool <br/> exec. submit (new runner (barrier, 0, new int [] {4, 8, 12}); <br/> exec. submit (new runner (barrier, 1, new int [] {1, 2, 3}); <br/> exec. submit (new runner (barrier, 2, new int [] {2, 4, 6}); </P> <p> // destroy the thread pool <br/> exec. shutdown (); <br/>}</P> <p >}< br/> class runner implements runnable <br/>{< br/> private int NO = 0; // thread serial number <br/> private int [] times = NULL; // time required to arrive at each set point, in seconds <br/> private javasicbarrier barrier = NULL; // synchronization secondary object </P> <p> Public runner (icicbarrier barrier, int No, int [] times) {<br/> This. times = times; <br/> This. no = no; <br/> This. barrier = barrier; <br/>}</P> <p> Public void run () {<br/> try {<br/> thread. sleep (Times [0] * 1000); <br/> system. out. println (new date () + "thread _" + NO + "arrives at Point 1"); <br/> barrier. await (); </P> <p> thread. sleep (Times [1] * 1000); <br/> system. out. println (new date () + "thread _" + NO + "arrives at Point 2"); <br/> barrier. await (); </P> <p> thread. sleep (Times [2] * 1000); <br/> system. out. println (new date () + "thread _" + NO + "arrives at point 3"); <br/> barrier. await (); <br/>}< br/> catch (interruptedexception bke) {<br/> // todo: handle exception <br/> bke. printstacktrace (); <br/>}< br/> catch (brokenbarrierexception bke) {<br/> // todo: handle exception <br/> bke. printstacktrace (); <br/>}< br/>}

Running result:

Thu Jan 20 09:04:40 CST 2011thread_1 arrives at Point 1 </P> <p> Thu Jan 20 09:04:41 CST 2011thread_2 arrives at Point 1 </P> <p> Thu Jan 20 09:04:43 CST 2011thread_0 arrives at Point 1 </P> <p> Thu Jan 20 09:04:45 CST 2011thread_1 arrives at Point 2 </P> <p> Thu Jan 20 09:04:47 CST 2011thread_2 arrives at Point 2 </P>/ p> <p> Thu Jan 20 09:04:51 CST 2011thread_0 arrives at Point 2 </P> <p> Thu Jan 20 09:04:54 CST 2011thread_1 arrives at point 3 </P> <p> thu jan 20 09:04:57 CST 2011thread_2 arrives at point 3 </P> <p> Thu Jan 20 09:05:03 CST 2011thread_0 arrives at point 3 </P> <p>

Countdownlatch

Java. util. Concurrent. countdownlatch is another thread synchronization helper class. Its role is basically the same as that of javasicbarrier. It also locks itself until other threads reach the "conset point" to continue execution. Otherwise, the thread waits for other threads at the assembly point. But there are also differences, both of which consider that the basis for all threads to be in the "Consortium" is different. Cyclicbarrier determines whether to unlock the Lock Based on whether the number of threads reaching the "conset point" has reached the number of threads to be synchronized. Countdownlatch determines whether to unlock the counter based on whether the counter is 0.

The Code testcountdownlatch is provided below]

/** <Br/> * testcountdownlatch. java <br/> * copyright (c) 2011 cuiran2001@163.com <br/> * Create: cui ran 2011-1-14 02:34:54 PM <br/> */</P> <p> package COM. cayden. thread832; </P> <p> Import Java. util. date; <br/> Import Java. util. concurrent. countdownlatch; <br/> Import Java. util. concurrent. executorservice; <br/> Import Java. util. concurrent. executors; </P> <p>/** <br/> * @ author Cui ran <br/> * @ version 1.0.0 <br/> * @ DESC <br/> */ <br/> public class testcountdownlatch {</P> <p>/** <br/> * @ Param ARGs <br/> * @ throws interruptedexception <br/> */ <br/> Public static void main (string [] ARGs) throws interruptedexception {<br/> // synchronization Helper Object with todo auto-generated method stub <br/> // Count value 1 <br/> final countdownlatch begin = new countdownlatch (1 ); <br/> // synchronization secondary object with a Count value of 10 <br/> final countdownlatch end = new countdownlatch (10 ); </P> <p> // thread pool with a capacity of 10 <br/> executorservice exec = executors. newfixedthreadpool (10); </P> <p> for (INT I = 0; I <10; I ++) {<br/> runnable run = new runnable () {<br/> Public void run () {<br/> try {<br/> begin. await (); <br/> system. out. println (new date () + "" + thread. currentthread (). getname () + "start running"); </P> <p> thread. sleep (long) (math. random () * 10000); </P> <p> system. out. println (new date () + "" + thread. currentthread (). getname () + "finished"); </P> <p >}catch (interruptedexception e) {<br/> // todo: handle exception <br/>}finally {<br/> end. countdown (); <br/>}< br/>}; <br/> exec. submit (run); <br/>}< br/> begin. countdown (); <br/> end. await (); <br/> system. out. println (new date () + "" + thread. currentthread (). getname () + "finished"); <br/> exec. shutdown (); <br/>}</P> <p >}< br/>

Running result:

Thu Jan 20 09:48:45 CST 2011 pool-1-thread-10 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-9 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-1 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-7 start run </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-5 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1- start running thread-6 </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-8 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-4 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-3 start running </P> <p> Thu Jan 20 09:48:45 CST 2011 pool-1-thread-2 start running </P> <p> Thu Jan 20 09:48:49 CST 2011 pool-1-thread-1 run completed </P> <p> Thu Jan 20 09:48:49 CST 2011 pool-1-thread-4 running completed </P> <p> Thu Jan 20 09:48:50 CST 2011 pool-1-thread-5 running completed </P> <p> Thu Jan 20 09:48:50 CST 2011 pool-1-thread-3 run completed </P> <p> Thu Jan 20 09:48:50 CST 2011 pool-1-thread- 2 run completed </P> <p> Thu Jan 20 09:48:51 CST 2011 pool-1-thread-7 run completed </P> <p> Thu Jan 20 09:48:52 CST 2011 pool- 1-thread-10 finished </P> <p> Thu Jan 20 09:48:54 CST 2011 pool-1-thread-8 finished </P> <p> Thu Jan 20 09:48:55 CST 2011 pool-1-thread-6 running completed </P> <p> Thu Jan 20 09:48:55 CST 2011 pool-1-thread-9 running completed </P> <p> thu Jan 20 09:48:55 CST 2011 main finished running <br/>

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.