Cyclicbarrier can implement multiple threads waiting each other, and when each thread arrives at a point, it blocks until all the threads are complete before continuing
Cyclicbarrier and the last introduction of the Countdownlatch somewhat similar, but there are different, the main differences are as follows:
Cyclicbarrier are multiple threads waiting for each other, Countdownlatch is a thread waiting for multiple threads cyclicbarrier can be reused, and countdownlatch can not
Similar to Countdownlatch, there is a scenario where multiple threads are now required to perform a statistical task, and after each thread has counted, it is necessary to wait for another thread to complete before continuing the rollup task of this thread, looking at the code
public class Cyclicbarriertest {public static void main (string[] args) {cyclicbarrier barrier = new Cyclicbarrier (5)
;
for (int i = 0; i < 5; i++) {Thread t = new Thread (new Task (barrier));
T.start ();
}} class Task implements Runnable {private cyclicbarrier barrier;
Public Task (Cyclicbarrier barrier) {this.barrier = barrier;
@Override public void Run () {try {thread.sleep (int) (Math.random () * 10) * 1000);
SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "End statistic task, wait for other thread to finish ...");
Barrier.await ();//After the method is invoked, it will stop running until the other threads have finished Thread.Sleep ((int) (Math.random () * 10) * 1000);
SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "start summarizing ...");
catch (Interruptedexception e) {e.printstacktrace ();
catch (Brokenbarrierexception e) {e.printstacktrace (); }}//results as follows//thread Thread-2 End statistic task, wait for other thread to finish .../thread Thread-1 end statistic task, wait for other thread to finish .../thread Thread-3 end statistic task, wait for other thread to finish ...//thread Thread-0 End statistic task waiting for other threads to complete...//thread Thread-4 End statistic task, wait for other thread to finish ...//thread Thread-1 start rollup ...//thread Thread-0 start summarizing ... thread Thread-4 start rollup ...//thread Threa D-2 Start the rollup ...//thread Thread-3 start the rollup ....After calling the Cyclicbarrier await method, the line Cheng waits until all other threads arrive at this point, continues to run if all threads arrive, and then waits until all arrives
Look at another example, and deepen the impression: there is a meeting that needs to be started after everyone arrives, and all the people can speak after the meeting starts.
Look at the code.
public class Cyclicbarrierdemo {public static void main (string[] args) {cyclicbarrier barrier = new Cyclicbarrier (5,
New Conference ());
for (int i = 0; i < 5; i++) {New Thread (new Paticipant (barrier, "actor" + i)). Start ();
} public static class Paticipant implements Runnable {private cyclicbarrier barrier;
private String name;
Public paticipant (cyclicbarrier barrier, String name) {this.barrier = barrier;
THIS.name = name;
@Override public void Run () {try {thread.sleep (int) (Math.random () * 10) * 1000);
SYSTEM.OUT.PRINTLN (name + "arrival ...");
Barrier.await ();
Thread.Sleep ((int) (Math.random () * 10) * 1000);
SYSTEM.OUT.PRINTLN (name + "make a statement");
catch (Interruptedexception e) {e.printstacktrace ();
catch (Brokenbarrierexception e) {e.printstacktrace (); }} public static class conference implements Runnable {@Override public void run () {System.out.println ("
Everyone is here, the meeting begins, the participant speaks "); }
}
}
//results as follows//Participant 2 arrives ...//Participant 3 arrives ....//Participant 0 arrives ...//Participant 4 arrives ...//participant 1 arrives ...//all of us arrive, the meeting begins, participants speak on//participants 4 speak//Participant 3 Participant 1 Presentations//Participant 2 presentations//Participant 0 statementsHere the Cyclicbarrier constructor passes in a Runnable object that executes the run method of the object when all threads arrive, and then continues to run the code behind each thread