zoukankan      html  css  js  c++  java
  • java 多线程 day15 CyclicBarrier 路障


    import java.util.concurrent.CyclicBarrier;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class Thread1501_CyclicBarrier {
    public static void main(String[] args) {

    System.out.println(" 主线程开始执行 -----");
    ExecutorService service = Executors.newCachedThreadPool();
    final CyclicBarrier cb = new CyclicBarrier(3);
    for(int i=0;i<3;i++){
    Runnable runnable = new Runnable(){
    public void run(){
    try {
    Thread.sleep((long)(Math.random()*10000));
    System.out.println("线程" + Thread.currentThread().getName() +
    "即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
    cb.await();

    Thread.sleep((long)(Math.random()*10000));
    System.out.println("线程" + Thread.currentThread().getName() +
    "即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
    cb.await();
    Thread.sleep((long)(Math.random()*10000));
    System.out.println("线程" + Thread.currentThread().getName() +
    "即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
    cb.await();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    };
    service.execute(runnable);
    }
    service.shutdown();
    System.out.println(" 主线程执行结束 -----");
    }
    }
  • 相关阅读:
    zabbix监控windows案例
    Ansible自动化运维工具-上
    Nagios监控的部署与配置
    ELK+Redis+Nginx服务数据存储以及Nginx日志的收集
    ELK5.3日志分析平台&部署
    Centos7X部署Zabbix监控
    LVS+Keepalived负载均衡
    Nginx+tomcat负载均衡
    smokeping
    Err.number错误号和可捕获的 Microsoft access 数据库引擎和 DAO错误说明
  • 原文地址:https://www.cnblogs.com/ctaixw/p/7980651.html
Copyright © 2011-2022 走看看