美文网首页
线程池模拟高并发

线程池模拟高并发

作者: JPhoebe | 来源:发表于2018-01-22 16:23 被阅读0次

    Integer taskCount =700;

    // 锁住所有线程,等待并发执行

    final CountDownLatch begin =new CountDownLatch(1);

    final ExecutorService exec =Executors.newFixedThreadPool(taskCount);

    for (int index =0; index < taskCount; index++){

    final int NO= index +1;

        Runnable run =new Runnable() {

    @Override

            public void run() {

    try {

    // 等待,所有一起执行

                    begin.await();

                    //开始模拟等待。。。

                    //Thread.sleep((long) (Math.random() * 10000));

                }catch (InterruptedException e){

    e.printStackTrace();

                }

    finally {

    }

    }

    };

        exec.submit(run);

    }

    System.out.println("开始执行");

    // begin减一,开始并发执行

    begin.countDown();

    //关闭执行

    exec.shutdown();

    相关文章

      网友评论

          本文标题:线程池模拟高并发

          本文链接:https://www.haomeiwen.com/subject/rezlaxtx.html