12.线程通信CyclicBarrier

Posted 永恒之心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.线程通信CyclicBarrier相关的知识,希望对你有一定的参考价值。

CountDownLatch

    监听某个线程的初始化,等待初始化执行完毕后,通知主线程工作。延迟、阻塞的是主线程,在单个线程中。

CyclicBarrier

    针对多个线程、线程池,多个线程初始化准备之后,去操作同一件事件(必须一定要所有准备才可执行,通知各的主线程去执行正常工作)。

  1. package demo2;
  2. import java.util.Random;
  3. import java.util.concurrent.BrokenBarrierException;
  4. import java.util.concurrent.CyclicBarrier;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. /**
  8. * Created by liudan on 2017/7/30.
  9. */
  10. public class DemoCyclicBarrier {
  11. /**
  12. * 假设一个场景:3个运动员,当跑步运动员准备好了,才一起出发
  13. */
  14. static class Runner implements Runnable {
  15. private CyclicBarrier CYCLIC_BARRIER;
  16. private String name;
  17. public Runner(CyclicBarrier CYCLIC_BARRIER, String name) {
  18. this.CYCLIC_BARRIER = CYCLIC_BARRIER;
  19. this.name = name;
  20. }
  21. @Override
  22. public void run() {
  23. try {
  24. Thread.sleep(1000 * (new Random().nextInt(5)));
  25. System.err.println(name + "\t准备好了");
  26. CYCLIC_BARRIER.await();
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. } catch (BrokenBarrierException e) {
  30. e.printStackTrace();
  31. }
  32. System.err.println("Go!");
  33. }
  34. public static void main(String[] args) {
  35. CyclicBarrier CYCLIC_BARRIER = new CyclicBarrier(3);
  36. ExecutorService executorService = Executors.newFixedThreadPool(3);
  37. executorService.execute(new Runner(CYCLIC_BARRIER, "1号"));
  38. executorService.execute(new Runner(CYCLIC_BARRIER, "2号"));
  39. executorService.execute(new Runner(CYCLIC_BARRIER, "2号"));
  40. executorService.shutdown();
  41. }
  42. }
  43. }
  44. 输出:
  45. 1号 准备好了 2号 准备好了 2号 准备好了 Go! Go! Go!

案例2
  1. public class TestCyclicBarrier {
  2. private static final int THREAD_NUM = 5;
  3. public static class WorkerThread implements Runnable{
  4. CyclicBarrier barrier;
  5. public WorkerThread(CyclicBarrier b){
  6. this.barrier = b;
  7. }
  8. @Override
  9. public void run() {
  10. // TODO Auto-generated method stub
  11. try{
  12. System.out.println("Worker‘s waiting");
  13. //线程在这里等待,直到所有线程都到达barrier。
  14. barrier.await();
  15. System.out.println("ID:"+Thread.currentThread().getId()+" Working");
  16. }catch(Exception e){
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. /**
  22. * @param args
  23. */
  24. public static void main(String[] args) {
  25. // TODO Auto-generated method stub
  26. CyclicBarrier cb = new CyclicBarrier(THREAD_NUM, new Runnable() {
  27. //当所有线程到达barrier时执行
  28. @Override
  29. public void run() {
  30. // TODO Auto-generated method stub
  31. System.out.println("Inside Barrier");
  32. }
  33. });
  34. for(int i=0;i<THREAD_NUM;i++){
  35. new Thread(new WorkerThread(cb)).start();
  36. }
  37. }
  38. }
  39. /*
  40. 以下是输出:
  41. Worker‘s waiting
  42. Worker‘s waiting
  43. Worker‘s waiting
  44. Worker‘s waiting
  45. Worker‘s waiting
  46. Inside Barrier
  47. ID:12 Working
  48. ID:8 Working
  49. ID:11 Working
  50. ID:9 Working
  51. ID:10 Working
  52. */





以上是关于12.线程通信CyclicBarrier的主要内容,如果未能解决你的问题,请参考以下文章

当通过的线程数量小于屏障限制时,屏障(例如 CyclicBarrier)是不是会导致死锁?

使用CyclicBarrier模拟线程并发执行代码

Java并发多线程编程——CyclicBarrier

25.大白话说java并发工具类-CountDownLatch,CyclicBarrier,Semaphore,Exchanger

CyclicBarrier源码分析

Java 里如何实现线程间通信