• 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 + " 准备好了");
    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. */



  • 相关阅读:
    less中遇到的一些特殊的写法
    观察者模式
    发布订阅模式
    javascript中的函数
    关于js中this的理解和作用
    [Oracle]Oracle镜像安装及使用教程
    log4net日志配置
    过滤器
    .net core 处理 中文乱码
    Fromform
  • 原文地址:https://www.cnblogs.com/xxt19970908/p/7302376.html
Copyright © 2020-2023  润新知