Package Org.rui.thread.newc;import Java.util.arraylist;import Java.util.list;import java.util.random;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.TimeUnit ;/** * Horse racing game Cyclicbarrier * * @author Lenovo * */class Horse implements Runnable {private static int counter = 0;private Final int id = counter++;p rivate int strides = 0;private static Random rand = new random;p rivate static cyclicbarrier Barrier;public Horse (Cyclicbarrier b) {barrier = b;} public synchronized int Getstrides () {return strides;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {synchronized (this) {//each game will be preceded by a randomly crossed number 0,1 or 2strides + + rand.nextint (3);//produces 0,1 or 2}// Wait until all the horses are ready, and when all the horses move forward, Cyclicbarrier will automatically invoke the Runnable Fence Action task, showing the position of the horse and end point in order barrier.await ();}} catch (Interruptedexception e) {} catch (Brokenbarrierexception e) {throw new RuntimeexCeption (e);}} Public String toString () {return "Horse" + ID + "";} Track public String tracks () {StringBuilder s = new StringBuilder (), for (int i = 0; i < getstrides (); i++) {S.append ("*" );} S.append (ID); return s.tostring ();}} public class Horserace {static final int finish_line = 15;//end Line private list... The print line is omitted here ===========================================================================***************************** 0*******************************************************1************ 2******************************************************** 3***********************************************************************4********************** 5************************************************************* 6===========================================================================*********************************** 0*******************************************************1***************** 2************************************************************ 3***********************************************************************4************************ **5*************************************************************** 6===========================================================================*********************************** 0********************************************************1*************** 2********************************************************** 3***********************************************************************4******************** 5******************************************************* 6HORSE3 win! */
An easy-to-understand Zhang Xiaoxiang teacher explained the example
Package Test;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * CyclicBarrier application * @author Lenovo * * * * public class Cyclicbarriertest {public static void main (string[] args) {Executorservice service = Executors.newcachedthre Adpool (); final Cyclicbarrier cb = new Cyclicbarrier (3); Three threads arrive at the same time for (int i = 0; i < 3; i++) {Runnable Runnable = new Runnable () {public void run () {try {Thread.Sleep ((long) (Math.random () * 10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Approaching meeting point 1, currently available" + (cb.getnumberwaiting () + 1) + "reached" + ( cb.getnumberwaiting () = = 2? "All is up, go on": "Waiting"), try {cb.await ();} catch (Brokenbarrierexception e) {e.printstacktrace ();} Thread.Sleep ((Long) (Math.random () * 10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Approaching meeting point 2, currently available" + (cb.getnumberwaiting () + 1) + "reached" + ( cb.getnumberwaiting () = = 2? "It's all here, keep going," said theWait ")), try {cb.await ();} catch (Brokenbarrierexception e) {e.printstacktrace ();} Thread.Sleep ((Long) (Math.random () * 10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Approaching meeting point 3, currently available" + (cb.getnumberwaiting () + 1) + "reached" + ( cb.getnumberwaiting () = = 2? "It's all here, keep going": "Waiting"), try {cb.await ();} catch (Brokenbarrierexception e) {e.printstacktrace ()}} catch (Interruptedexception e) {e.printstacktrace ();}}}; Service.execute (runnable);} Service.shutdown ();}} /** * Output: Thread pool-1-thread-3 is about to arrive at meeting point 1, there are currently 1 arrived waiting thread pool-1-thread-1 will arrive at meeting Place 1, There are currently 2 arrived waiting thread pool-1-thread-2 is about to arrive at the meeting point 1, there are currently 3 have arrived to all, continue to go AH thread pool-1-thread-2 will arrive at the meeting place 2, There are currently 1 arrived waiting thread pool-1-thread-1 is about to arrive at the meeting place 2, currently 2 have arrived waiting for thread pool-1-thread-3 is about to arrive at the meeting place 2, there are currently 3 have arrived to the point, Keep going. Thread pool-1-thread-3 is about to arrive at meeting point 3, there are currently 1 arrived at the waiting thread pool-1-thread-2 is about to arrive at the meeting point 3, there are currently 2 arrived waiting thread pool-1-thread-1 will arrive at the meeting place 3 , there are currently 3 have arrived to all, continue to go AH * *
Java thread assigns component Cyclicbarrier use in class library