CyclicBarrier和CountDownLatch的区别

来源:互联网 发布:王者荣耀用java还是c 编辑:程序博客网 时间:2024/05/29 03:20
1. 字面意思
CyclicBarrier : 循环的屏障
CountDownLatch : 向下计数锁

2. 用途
CyclicBarrier : 让指定的线程等待,直到所有线程达到同一执行点全部继续执行
CountDownLatch : 让线程等待,直到CountDownLatch计数到0,继续执行该线程。

3. 主要方法:
CyclicBarrier :
await() : 一直等待,直到所有线程都执行到await代码处,所有线程同时继续执行。
CountDownLatch:
countDown : 计数-1
await() : 线程等待,直到计数到0才继续执行

4. 实例:
CyclicBarrier:
public class CyclicBarrierTest {public static void main(String[] args) {CyclicBarrier cyclicBarrier = new CyclicBarrier(10);for(int i = 0; i < 10;i++) {Thread t = new Thread(new ThreadTest2(cyclicBarrier, i * 1000L));t.start();}}}class ThreadTest2 implements Runnable {CyclicBarrier cyclicBarrier;long s = 0;public ThreadTest2(CyclicBarrier cyclicBarrier, long s) {this.cyclicBarrier = cyclicBarrier;this.s = s;}@Overridepublic void run() {System.out.println("start current thread name : " + Thread.currentThread().getName());try {Thread.sleep(s);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("current thread name : " + Thread.currentThread().getName() + "end sleep");try {cyclicBarrier.await();} catch (InterruptedException e1) {e1.printStackTrace();} catch (BrokenBarrierException e1) {e1.printStackTrace();}System.out.println("end current thread name : " + Thread.currentThread().getName());}}


结果:
start current thread name : Thread-3
start current thread name : Thread-2
start current thread name : Thread-0
start current thread name : Thread-1
start current thread name : Thread-5
start current thread name : Thread-4
start current thread name : Thread-6
start current thread name : Thread-7
start current thread name : Thread-8
current thread name : Thread-0end sleep
start current thread name : Thread-9
current thread name : Thread-1end sleep
current thread name : Thread-2end sleep
current thread name : Thread-3end sleep
current thread name : Thread-4end sleep
current thread name : Thread-5end sleep
current thread name : Thread-6end sleep
current thread name : Thread-7end sleep
current thread name : Thread-8end sleep
current thread name : Thread-9end sleep
end current thread name : Thread-9
end current thread name : Thread-8
end current thread name : Thread-7
end current thread name : Thread-6
end current thread name : Thread-5
end current thread name : Thread-0
end current thread name : Thread-4
end current thread name : Thread-3
end current thread name : Thread-2
end current thread name : Thread-1

分析:
main线程中启动10个子线程,并传入CyclicBarrier对象,子线程立即打印start current name,
随后执行睡眠(sleep),结束睡眠后打印end sleep ,虽然不同的子线程睡眠时间不一样,但由于调用 了
cyclicBarrier.await()方法,所有子线程都等待其它线程都执行到该代码处才继续执行打印end current name

CountDownLatch:
public class CountDownLatchTest {public static void main(String[] args) throws InterruptedException {System.out.println("main thread start");CountDownLatch latch = new CountDownLatch(10);for(int i = 0; i < 10; i++) {ThreadTest t = new ThreadTest(latch);Thread thread = new Thread(t);thread.start();}latch.await();System.out.println("main thread end");}}class ThreadTest implements Runnable {CountDownLatch latch;public ThreadTest(CountDownLatch latch) {this.latch = latch;}@Overridepublic void run() {System.out.println("start current thread name : " + Thread.currentThread().getName());latch.countDown();try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("end current thread name : " + Thread.currentThread().getName());}}


结果:
main thread start
start current thread name : Thread-3
start current thread name : Thread-1
start current thread name : Thread-0
start current thread name : Thread-2
start current thread name : Thread-4
start current thread name : Thread-5
start current thread name : Thread-6
start current thread name : Thread-7
start current thread name : Thread-8
start current thread name : Thread-9
main thread end

分析:
main线程打印main thread start,随后启动10个子线程,启动完线程后由于调用了latch.await()方法,所以并没有立既继续执行main thread end,而是等待子线程调用latch.countDown()方法使计数到0后才继续执行main thread end
原创粉丝点击