1.1.
circulation barrier cyclicbarrier
Cyclicbarrier is used to synchronize multiple threads at a synchronization point. A thread that has reached the synchronization point waits for another thread before all threads reach the synchronization point. A simple comprehension can be said that cyclicbarrier is similar to traveling with a tour group. Before all the team members arrive at the departure meeting point, the team member can only wait for the other team members at the meeting point. When all the players have reached, they can go. The loop barrier can be similar to the tour group waiting at each meeting place, and then unify to the next attraction.
Packagecom.test.concurrence;Importjava.util.Date;ImportJava.util.Random;Importjava.util.concurrent.BrokenBarrierException;ImportJava.util.concurrent.CyclicBarrier;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classCyclicbarriertest { Public Static voidMain (string[] args) {//A total of 10 parties: Here are 10 threads. FinalCyclicbarrier Cyclicbarrier =NewCyclicbarrier (10); Executorservice Executorservice= Executors.newfixedthreadpool (10); for(inti=0;i<10;i++) {Executorservice.submit (NewRunnable () {@Override Public voidrun () {Try { //randomly paused for a period of time, simulated ahead of reach. Thread.Sleep (1000*NewRandom (). Nextint (20)); System.out.println ("Waiting:" +cyclicbarrier.getnumberwaiting ()+ ", Parties:" +cyclicbarrier.getparties ()+ " , " +NewDate ()); //the prematurely reached thread waits for a thread that has not yet arrived. cyclicbarrier.await ();} Catch(Interruptedexception |brokenbarrierexception e) {E.printstacktrace ();}//all the threads are here. System.out.println ("Ready:" +cyclicbarrier.getnumberwaiting ()+ ", Parties:" +cyclicbarrier.getparties ()+ " , " +NewDate ()); } });} Executorservice.shutdown (); } }
The results of the operation are as follows:
waiting:0, Parties:10, Sat Mar 22:40:06 CST 2017
waiting:0, Parties:10, Sat Mar 22:40:06 CST 2017
waiting:0, Parties:10, Sat Mar 22:40:06 CST 2017
Waiting:3, Parties:10, Sat Mar 22:40:10 CST 2017
Waiting:4, Parties:10, Sat Mar 22:40:13 CST 2017
Waiting:5, Parties:10, Sat Mar 22:40:19 CST 2017
Waiting:5, Parties:10, Sat Mar 22:40:19 CST 2017
Waiting:7, Parties:10, Sat Mar 22:40:22 CST 2017
Waiting:8, Parties:10, Sat Mar 22:40:23 CST 2017
Waiting:8, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
ready:0, Parties:10, Sat Mar 22:40:23 CST 2017
the Cyclicbarrier constructor can receive a Runnable object that executes the Run method of this object when all threads are reached .
Packagecom.test.concurrence;Importjava.util.Date;ImportJava.util.Random;Importjava.util.concurrent.BrokenBarrierException;ImportJava.util.concurrent.CyclicBarrier;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classCyclicBarrierTest2 { Public Static voidMain (string[] args) {FinalCyclicbarrier Cyclicbarrier =NewCyclicbarrier (10,NewRunnable () {@Override Public voidrun () {System.out.println ("Ready" +NewDate ()); } }); Executorservice Executorservice= Executors.newfixedthreadpool (10); for(inti=0;i<10;i++) {Executorservice.submit (NewRunnable () {@Override Public voidrun () {Try{Thread.Sleep (1000*NewRandom (). Nextint (20)); System.out.println ("Waiting:" +cyclicbarrier.getnumberwaiting ()+ ", Parties:" +cyclicbarrier.getparties ()+ " , " +NewDate ()); cyclicbarrier.await ();} Catch(Interruptedexception |brokenbarrierexception e) {E.printstacktrace ();} } });} Executorservice.shutdown (); } }
waiting:0, Parties:10, Sat Mar 22:59:54 CST 2017
Waiting:1, Parties:10, Sat Mar 22:59:58 CST 2017
Waiting:2, Parties:10, Sat Mar 22:59:59 CST 2017
Waiting:3, Parties:10, Sat Mar 23:00:01 CST 2017
Waiting:4, Parties:10, Sat Mar 23:00:02 CST 2017
Waiting:5, Parties:10, Sat Mar 23:00:03 CST 2017
Waiting:6, Parties:10, Sat Mar 23:00:06 CST 2017
Waiting:7, Parties:10, Sat Mar 23:00:07 CST 2017
Waiting:8, Parties:10, Sat Mar 23:00:08 CST 2017
Waiting:9, Parties:10, Sat Mar 23:00:11 CST 2017
Ready Sat Mar 23:00:11 CST 2017
Java concurrency Programming (19) Cyclicbarrier of the cyclic barrier of concurrent lock