package com.smart.thread;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
/**
CyclicBarrier可以在构造时指定需要在屏障前执行await的个数,所有对await的调用都会等待,
直到调用await的次数达到预定指,所有等待都会立即被唤醒。
从使用场景上来说,CyclicBarrier是让多个线程互相等待某一事件的发生,然后同时被唤醒。
而CountDownLatch是让某一线程等待多个线程的状态,然后该线程被唤醒。
-
Created by jinxiaoyu on 17/4/11.
*/
public class CyclicBarrierDemo {
public static void main(String[] args) throws InterruptedException {
int totalThread = 5;
final CyclicBarrier barrier = new CyclicBarrier(totalThread);for(int i = 0; i < totalThread; i++) { final String threadName = "Thread " + i; new Thread() { @Override public void run() { System.out.println(String.format("%s\t%s %s", new Date(), threadName, " is waiting")); try { barrier.await(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(String.format("%s\t%s %s", new Date(), threadName, "ended")); } }.start(); }
}
}
Tue Apr 11 21:30:19 CST 2017 Thread 1 is waiting
Tue Apr 11 21:30:19 CST 2017 Thread 0 is waiting
Tue Apr 11 21:30:19 CST 2017 Thread 4 is waiting
Tue Apr 11 21:30:19 CST 2017 Thread 3 is waiting
Tue Apr 11 21:30:19 CST 2017 Thread 2 is waiting
Tue Apr 11 21:30:19 CST 2017 Thread 1 ended
Tue Apr 11 21:30:19 CST 2017 Thread 2 ended
Tue Apr 11 21:30:19 CST 2017 Thread 0 ended
Tue Apr 11 21:30:19 CST 2017 Thread 4 ended
Tue Apr 11 21:30:19 CST 2017 Thread 3 ended