zoukankan      html  css  js  c++  java
  • CountDownLatch

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class TestCountDownLatch {

    public static void main(String[] args) throws InterruptedException {
    long start = System.currentTimeMillis();
    // 开始的倒数锁
    final CountDownLatch begin = new CountDownLatch(1);
    // 结束的倒数锁
    final CountDownLatch end = new CountDownLatch(10000);
    List<String> list=new ArrayList<String>();
    // 十名选手
    final ExecutorService exec = Executors.newFixedThreadPool(10);
    for (int index = 0; index < 10000; index++) {
    final int NO = index;
    Runnable run = new Runnable() {
    public void run() {
    try {
    begin.await();
    System.out.println(NO);
    String temp="AA"+NO;
    list.add(temp);
    Thread.sleep((long) (Math.random() * 10));
    } catch (InterruptedException e) {
    } finally {
    end.countDown();
    }
    }
    };
    System.out.println(run);
    exec.submit(run);
    }
    System.out.println("Game Start");
    begin.countDown();
    end.await();
    System.out.println("Game Over");
    exec.shutdown();
    System.out.println("num:"+list.size());
    // for(String s:list){
    // System.out.println("数据打印:"+s);
    // }
    System.out.print("共计用时 ");
    System.out.println(System.currentTimeMillis() - start);
    }
    }

  • 相关阅读:
    制作Windows Server 2008安装启动U盘
    wireshark教程(一)
    TCPdump抓包命令详解
    ATM交换机 和普通交换机区别
    胖ap和瘦ap区别
    酒店网络非常常见故障一例
    JQuery EasyUI DataGrid动态合并(标题)单元) 一
    字典表左右选择
    treegrid-dnd.js
    MySQL开发规范
  • 原文地址:https://www.cnblogs.com/justuntil/p/9636087.html
Copyright © 2011-2022 走看看