Cyclicbarrier
Then the other components under multithreading, the first thing to say is Cyclicbarrier. Cyclicbarrier literally refers to the loop barrier, which can work with multiple threads, allowing multiple threads to wait in front of the barrier until all the threads have reached the barrier, and then continue to perform the subsequent actions together. Take a look at the usage examples of cyclicbarrier:
public Static classCyclicbarrierthreadextendsthread{PrivateCyclicbarrier cb; Private intsleepsecond; publicCyclicbarrierthread (cyclicbarrier cb,intSleepsecond) { this. cb =cb; this. Sleepsecond =sleepsecond; } public voidRun () {Try{System.out.println ( this. getName () + "ran"); Thread.Sleep (sleepsecond* 1000); System.out.println ( this. getName () + "ready to wait, time for" +System.currenttimemillis ()); Cb.await (); System.out.println ( this. getName () + "end of wait, time" +System.currenttimemillis ()); } Catch(Exception E) {e.printstacktrace (); } }} public Static voidmain (string[] Args) {Runnable Runnable=NewRunnable () { public voidrun () {System.out.println ("cyclicbarrier All threads await () ended, i ran, time was" +System.currenttimemillis ()); } }; Cyclicbarrier CB=NewCyclicbarrier (3, runnable); Cyclicbarrierthread cbt0=NewCyclicbarrierthread (cb, 3); Cyclicbarrierthread cbt1=NewCyclicbarrierthread (cb, 6); Cyclicbarrierthread CBT2=NewCyclicbarrierthread (cb, 9); Cbt0.start (); Cbt1.start (); Cbt2.start ();}
Look at the results of the operation:
thread-0 Thread was run-2 thread was run-1 thread was run-0 ready to wait, time is 1444650316313Thread-1 ready to wait, time is 1444650319313Thread-2 ready to wait, all threads of time 1444650322313CyclicBarrier await () end, i run, time is 1444650322313Thread-2 End of wait, time is 1444650322313Thread-0 End of wait, time is 1444650322313Thread-1 End of wait, time is 1444650322313
From the results of the operation, as the same cyclicbarrier,thread-0 first run to await () place, wait, Thread-2 then run to await (), waiting, Thread-1 finally run to await () place, All threads run to the await (), so three threads and the specified runnable "simultaneously" run the following code, and you can see that after await (), four threads run exactly the same time, all 1444650322313.
from the perspective of use, it may be felt that Cyclicbarrier and countdownlatch a bit like, are many threads waiting for each other to complete, and then execute the following Code. In fact, Countdownlatch and cyclicbarrier are used for coordination between multiple threads, and there are several differences between them:
1, Countdownlatch is to trigger the event after multiple threads have been Latch.countdown (), wake the thread on the latch of await (), and execute Countdown () thread, after executing countdown () will continue to work on its own thread; countdownlatch and cyclicbarrier initialization are poor Not many, it is necessary to pass in a data token wait count number, different is the cyclicbarrier more than one thread task passed in as a thread that was awakened to execute
2, another countdownlatch and cyclicbarrier a difference is that countdownlatch can not be recycled, the counter minus 0 is reduced to 0, can not be reset, cyclicbarrier can be reused, call the reset method, The await thread is then set.
3, Countdownlatch can evoke multiple threads of the task, Cyclicbarrier can only evoke a thread of the task
Note that because threads that use cyclicbarrier block on the await method, use Cyclicbarrier in the thread pool with great care, and if the thread pool has too few threads, a deadlock will occur
Java multi-threading 30: Cyclicbarrier of multithreaded components