import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws Exception {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse1")));
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse2")));
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse3")));
executorService.shutdown();
}}
class Runner implements Runnable {
private CyclicBarrier cyclicBarrier;
private String name;
public Runner(CyclicBarrier cyclicBarrier, String name) {
super();
this.name = name;
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
try {
Thread.sleep(1000 * (new Random()).nextInt(10));
System.out.println(name + "准备好了");
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(name + "起跑");
}
}
执行结果:
只有当三匹马都准备好时候才能同时起跑。 另外: CyclicBarrier(n)控制的线程数n如果大于实际执行过程中的线程数,那么所有await的线程都将无限等待。
另外可以为cyclicBarrier设置超时时间,当线程等待时候超过了超时时间时候,就会不再等待,直接执行,CyclicBarrier抛出BrokenBarrierException 。其余应该受到cyclicBarrier约束的线程也因BrokenBarrierExceptio不再受到约束,转为直接执行。
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;
import java.util.concurrent.TimeoutException;
public class Main {
public static void main(String[] args) throws Exception {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse1")));
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse2")));
executorService.submit(new Thread(new Runner(cyclicBarrier, "horse3")));
executorService.shutdown();
}
}
class Runner implements Runnable {
private CyclicBarrier cyclicBarrier;
private String name;
public Runner(CyclicBarrier cyclicBarrier, String name) {
super();
this.name = name;
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
try {
long time = 1000 * (new Random()).nextInt(10);
Thread.sleep(time);
System.out.println(name + "准备好了,准备好之前睡了:" + time);
cyclicBarrier.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.out.println(name + "中断异常");
} catch (BrokenBarrierException e) {
System.out.println(name + "栅栏已经损坏了");
} catch (TimeoutException e) {
System.out.println(name + "等待超时");
}
System.out.println(name + "起跑");
}
}
另外存在这种场景CyclicBarrier控制n个线程,其中n-1个为副线程,另外一个为主线程。主线程等到其余n-1个线程执行完毕之后方可执行:
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws Exception {
CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
new Thread(new Runner(cyclicBarrier, "horse1")).start();
new Thread(new Runner(cyclicBarrier, "horse2")).start();
new Thread(new Runner(cyclicBarrier, "horse3")).start();
cyclicBarrier.await();
System.out.println("mainHorse start running after horse 1-3");
}
}
class Runner implements Runnable {
private CyclicBarrier cyclicBarrier;
private String name;
public Runner(CyclicBarrier cyclicBarrier, String name) {
super();
this.name = name;
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(name + "准备好了");
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}