public class Cyclicbarriertest {public static void main (string[] args) {Executorservice service = Executors.newcache
Dthreadpool ();
Final Cyclicbarrier cb = new Cyclicbarrier (3); for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try {thread.sleep (lon
g) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 1, there is currently" + (Cb.getnumberwaiting () +1) + "The one has been to DA, "+ (cb.getnumberwaiting () ==2?"
All here, keep walking ": Waiting");
Cb.await ();
Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 2, there is currently" + (Cb.getnumberwaiting () +1) + "The one has been to DA, "+ (cb.getnumberwaiting () ==2?"
All here, keep walking ": Waiting");
Cb.await ();
Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 3, currently has" + (cb.getnumberwaiting () + 1) + "A has been Upon arrival, "+ (Cb.getnumbeRwaiting () ==2? "
All here, keep walking ": Waiting");
Cb.await ();
catch (Exception e) {e.printstacktrace ();
}
}
};
Service.execute (runnable);
} service.shutdown ();
}
}
The results of the operation are as follows:
The thread pool-1-thread-3 is about to arrive at the rendezvous point 1, currently 1 have arrived and are waiting for
The thread pool-1-thread-1 is about to arrive at the rendezvous point 1, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 1, there are now 3 already arrived, are all here, keep going.
The thread pool-1-thread-3 is about to arrive at the Rendezvous point 2, currently 1 have arrived and are waiting for
The thread pool-1-thread-1 is about to arrive at the Rendezvous point 2, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 2, there are now 3 already arrived, are all here, keep going.
The thread pool-1-thread-1 is about to arrive at the rendezvous point 3, currently 1 have arrived and are waiting for
The thread pool-1-thread-3 is about to arrive at the rendezvous point 3, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 3, there are now 3 already arrived, are all here, keep going.
Organize from tutorial