码迷,mamicode.com
首页 > 编程语言 > 详细

006-多线程-JUC线程池-并发测试程序

时间:2019-08-06 21:20:40      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:countdown   void   rup   pos   守护线程   线程   允许   java代码   rri   

一、java代码模拟并发

1.1、一次并发

单次并发测试

1、使用CountDownLatch

  等待一个或多个线程一起执行

  详细参看:007-多线程-锁-JUC锁-CountDownLatch-闭锁【允许一个或多个线程,等待其他一组线程完成操作,再继续执行】

示例代码

    @Test
    public void poolCountDownLatch() throws Exception {
        int parrelnum=100;
        //所有线程阻塞,然后统一开始
        CountDownLatch begin = new CountDownLatch(1);
        //主线程阻塞,直到所有分线程执行完毕
        CountDownLatch end = new CountDownLatch(parrelnum);
        List<Future<String>> futureList = new ArrayList<>();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(parrelnum);
        for (int i = 0; i < parrelnum; i++) {
            Future<String> submit = fixedThreadPool.submit(() -> {
                try {
                    //begin 阻塞所有线程
                    begin.await();
                    System.out.println("======-----------------------------"+ LocalDateTime.now());
//                    System.out.println("======-----------------------------"+ LocalDateTime.now()
//                            +":"+Thread.currentThread().getName()
//                            +";守护线程:"+Thread.currentThread().isDaemon());
                    Thread.sleep(new Random().nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    //end 执行完代码减一
                    end.countDown();
                }
                return "ok:"+LocalDateTime.now();
            });
            futureList.add(submit);
        }
        fixedThreadPool.shutdown();

        try {
            System.out.println(LocalDateTime.now()+":"+"1秒后统一开始");
            Thread.sleep(1000);
            //begin 减一 = 0 唤醒 阻塞的所有线程
            begin.countDown();
            for (Future<String> future : futureList) {
                System.out.println(future.get());
            }
            //end 等待所有代码执行完毕
            end.await();
            System.out.println(LocalDateTime.now()+":"+"全部结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

输出:

技术图片
2019-08-02T22:58:23.762:1秒后统一开始
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.768
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.765
======-----------------------------2019-08-02T22:58:24.764
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.773
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.770
======-----------------------------2019-08-02T22:58:24.769
======-----------------------------2019-08-02T22:58:24.769
ok:2019-08-02T22:58:24.924
ok:2019-08-02T22:58:25.803
ok:2019-08-02T22:58:26.473
ok:2019-08-02T22:58:25.947
ok:2019-08-02T22:58:25.891
ok:2019-08-02T22:58:25.859
ok:2019-08-02T22:58:25.457
ok:2019-08-02T22:58:26.443
ok:2019-08-02T22:58:25.457
ok:2019-08-02T22:58:26.157
ok:2019-08-02T22:58:25.003
ok:2019-08-02T22:58:24.979
ok:2019-08-02T22:58:25.698
ok:2019-08-02T22:58:25.084
ok:2019-08-02T22:58:25.102
ok:2019-08-02T22:58:26.083
ok:2019-08-02T22:58:26.377
ok:2019-08-02T22:58:25.669
ok:2019-08-02T22:58:26.360
ok:2019-08-02T22:58:26.640
ok:2019-08-02T22:58:26.321
ok:2019-08-02T22:58:26.087
ok:2019-08-02T22:58:26.226
ok:2019-08-02T22:58:24.833
ok:2019-08-02T22:58:26.736
ok:2019-08-02T22:58:25.497
ok:2019-08-02T22:58:25.374
ok:2019-08-02T22:58:26.377
ok:2019-08-02T22:58:26.764
ok:2019-08-02T22:58:24.889
ok:2019-08-02T22:58:26.419
ok:2019-08-02T22:58:25.174
ok:2019-08-02T22:58:25.447
ok:2019-08-02T22:58:24.957
ok:2019-08-02T22:58:24.817
ok:2019-08-02T22:58:24.809
ok:2019-08-02T22:58:26.764
ok:2019-08-02T22:58:26.035
ok:2019-08-02T22:58:25.855
ok:2019-08-02T22:58:26.310
ok:2019-08-02T22:58:26.412
ok:2019-08-02T22:58:26.210
ok:2019-08-02T22:58:25.569
ok:2019-08-02T22:58:25.039
ok:2019-08-02T22:58:25.846
ok:2019-08-02T22:58:26.328
ok:2019-08-02T22:58:24.802
ok:2019-08-02T22:58:25.970
ok:2019-08-02T22:58:25.970
ok:2019-08-02T22:58:25.475
ok:2019-08-02T22:58:26.147
ok:2019-08-02T22:58:25.937
ok:2019-08-02T22:58:26.525
ok:2019-08-02T22:58:25.527
ok:2019-08-02T22:58:24.871
ok:2019-08-02T22:58:26.246
ok:2019-08-02T22:58:24.786
ok:2019-08-02T22:58:25.484
ok:2019-08-02T22:58:25.871
ok:2019-08-02T22:58:25.240
ok:2019-08-02T22:58:24.899
ok:2019-08-02T22:58:24.889
ok:2019-08-02T22:58:25.559
ok:2019-08-02T22:58:24.856
ok:2019-08-02T22:58:24.783
ok:2019-08-02T22:58:25.257
ok:2019-08-02T22:58:26.394
ok:2019-08-02T22:58:26.471
ok:2019-08-02T22:58:25.957
ok:2019-08-02T22:58:25.930
ok:2019-08-02T22:58:25.316
ok:2019-08-02T22:58:25.323
ok:2019-08-02T22:58:26.381
ok:2019-08-02T22:58:26.552
ok:2019-08-02T22:58:26.577
ok:2019-08-02T22:58:25.369
ok:2019-08-02T22:58:26.157
ok:2019-08-02T22:58:25.937
ok:2019-08-02T22:58:26.030
ok:2019-08-02T22:58:26.693
ok:2019-08-02T22:58:24.982
ok:2019-08-02T22:58:25.511
ok:2019-08-02T22:58:25.042
ok:2019-08-02T22:58:26.307
ok:2019-08-02T22:58:25.769
ok:2019-08-02T22:58:25.710
ok:2019-08-02T22:58:26.699
ok:2019-08-02T22:58:26.047
ok:2019-08-02T22:58:26.056
ok:2019-08-02T22:58:24.822
ok:2019-08-02T22:58:26.204
ok:2019-08-02T22:58:24.803
ok:2019-08-02T22:58:26.257
ok:2019-08-02T22:58:25.701
ok:2019-08-02T22:58:24.964
ok:2019-08-02T22:58:25.678
ok:2019-08-02T22:58:24.858
ok:2019-08-02T22:58:25.741
ok:2019-08-02T22:58:26.153
ok:2019-08-02T22:58:26.023
2019-08-02T22:58:26.766:全部结束
View Code

可以看到 100个线程并发,同时并发,64-73毫秒之间 即 9毫秒

2、不使用CountDownLatch

    @Test
    public void poolNoCountDownLatch() throws Exception {
        int parrelnum=100;
        List<Future<String>> futureList = new ArrayList<>();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(parrelnum);
        for (int i = 0; i < parrelnum; i++) {
            Future<String> submit = fixedThreadPool.submit(() -> {
                try {
                    System.out.println("======-----------------------------"+ LocalDateTime.now());
//                    System.out.println("======-----------------------------"+ LocalDateTime.now()
//                            +":"+Thread.currentThread().getName()
//                            +";守护线程:"+Thread.currentThread().isDaemon());
                    Thread.sleep(new Random().nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "ok:"+LocalDateTime.now();
            });
            futureList.add(submit);
        }
        fixedThreadPool.shutdown();

        try {
            System.out.println(LocalDateTime.now()+":"+"1秒后统一开始");
            Thread.sleep(1000);
            for (Future<String> future : futureList) {
                System.out.println(future.get());
            }
            System.out.println(LocalDateTime.now()+":"+"全部结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

可以看到 100个线程并发,同时并发,91-96毫秒之间 即 5毫秒

技术图片
com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.github.bjlhx15.common.base.thread.spring.controller.ThreadPoolExecutorParrelTest,poolNoCountDownLatch
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.491
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.492
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.494
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
2019-08-02T23:02:42.493:1秒后统一开始
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.493
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.496
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
======-----------------------------2019-08-02T23:02:42.495
ok:2019-08-02T23:02:43.998
ok:2019-08-02T23:02:43.772
ok:2019-08-02T23:02:42.961
ok:2019-08-02T23:02:42.623
ok:2019-08-02T23:02:44.241
ok:2019-08-02T23:02:43.478
ok:2019-08-02T23:02:44.477
ok:2019-08-02T23:02:43.033
ok:2019-08-02T23:02:43.533
ok:2019-08-02T23:02:44.004
ok:2019-08-02T23:02:42.856
ok:2019-08-02T23:02:44.217
ok:2019-08-02T23:02:44.190
ok:2019-08-02T23:02:44.011
ok:2019-08-02T23:02:42.603
ok:2019-08-02T23:02:44.448
ok:2019-08-02T23:02:44.017
ok:2019-08-02T23:02:42.833
ok:2019-08-02T23:02:42.808
ok:2019-08-02T23:02:44.479
ok:2019-08-02T23:02:43.207
ok:2019-08-02T23:02:44.190
ok:2019-08-02T23:02:42.677
ok:2019-08-02T23:02:43.624
ok:2019-08-02T23:02:42.848
ok:2019-08-02T23:02:44.161
ok:2019-08-02T23:02:43.904
ok:2019-08-02T23:02:44.439
ok:2019-08-02T23:02:44.361
ok:2019-08-02T23:02:43.598
ok:2019-08-02T23:02:43.777
ok:2019-08-02T23:02:43.153
ok:2019-08-02T23:02:42.922
ok:2019-08-02T23:02:43.466
ok:2019-08-02T23:02:43.689
ok:2019-08-02T23:02:43.881
ok:2019-08-02T23:02:42.784
ok:2019-08-02T23:02:43.598
ok:2019-08-02T23:02:42.691
ok:2019-08-02T23:02:43.671
ok:2019-08-02T23:02:43.920
ok:2019-08-02T23:02:43.833
ok:2019-08-02T23:02:44.415
ok:2019-08-02T23:02:43.019
ok:2019-08-02T23:02:44.244
ok:2019-08-02T23:02:42.963
ok:2019-08-02T23:02:44.361
ok:2019-08-02T23:02:43.689
ok:2019-08-02T23:02:43.620
ok:2019-08-02T23:02:42.753
ok:2019-08-02T23:02:43.055
ok:2019-08-02T23:02:43.259
ok:2019-08-02T23:02:43.837
ok:2019-08-02T23:02:44.233
ok:2019-08-02T23:02:43.483
ok:2019-08-02T23:02:42.753
ok:2019-08-02T23:02:43.823
ok:2019-08-02T23:02:44.448
ok:2019-08-02T23:02:44.361
ok:2019-08-02T23:02:43.727
ok:2019-08-02T23:02:42.641
ok:2019-08-02T23:02:43.648
ok:2019-08-02T23:02:43.282
ok:2019-08-02T23:02:42.522
ok:2019-08-02T23:02:44.394
ok:2019-08-02T23:02:43.037
ok:2019-08-02T23:02:44.366
ok:2019-08-02T23:02:44.093
ok:2019-08-02T23:02:43.494
ok:2019-08-02T23:02:44.120
ok:2019-08-02T23:02:44.283
ok:2019-08-02T23:02:43.917
ok:2019-08-02T23:02:44.093
ok:2019-08-02T23:02:43.524
ok:2019-08-02T23:02:42.593
ok:2019-08-02T23:02:43.628
ok:2019-08-02T23:02:42.544
ok:2019-08-02T23:02:43.279
ok:2019-08-02T23:02:44.182
ok:2019-08-02T23:02:42.878
ok:2019-08-02T23:02:44.177
ok:2019-08-02T23:02:43.371
ok:2019-08-02T23:02:42.538
ok:2019-08-02T23:02:43.783
ok:2019-08-02T23:02:43.515
ok:2019-08-02T23:02:43.616
ok:2019-08-02T23:02:43.241
ok:2019-08-02T23:02:43.366
ok:2019-08-02T23:02:42.731
ok:2019-08-02T23:02:43.844
ok:2019-08-02T23:02:43.423
ok:2019-08-02T23:02:43.698
ok:2019-08-02T23:02:43.689
ok:2019-08-02T23:02:43.733
ok:2019-08-02T23:02:43.459
ok:2019-08-02T23:02:43.546
ok:2019-08-02T23:02:44.067
ok:2019-08-02T23:02:44.494
ok:2019-08-02T23:02:44.054
ok:2019-08-02T23:02:43.923
2019-08-02T23:02:44.495:全部结束

Process finished with exit code 0
View Code

3、小结

故针对上述一次并发不推荐使用CountDownLatch增加程序复杂度

1.2、循环并发 

如,15个线程,每5个一组循环并发测试

    @Test
    public void poolCyclicBarrier() throws Exception {
        int parrelnum = 5;
        //所有线程阻塞,然后统一开始
        CyclicBarrier cyclicBarrier = new CyclicBarrier(parrelnum);
        List<Future<String>> futureList = new ArrayList<>();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(parrelnum);
        for (int i = 0; i < 15; i++) {
            Future<String> submit = fixedThreadPool.submit(() -> {
                try {
                    Thread.sleep(new Random().nextInt(2000));
                    //begin 阻塞所有线程
                    cyclicBarrier.await();
                    System.out.println("======-----------------------------并发:" + LocalDateTime.now());
                    Thread.sleep(new Random().nextInt(2000));
                    System.out.println("======-----------------------------" + LocalDateTime.now()
                            + ":" + Thread.currentThread().getName()
                            + ";守护线程:" + Thread.currentThread().isDaemon());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "ok:" + LocalDateTime.now();
            });
            futureList.add(submit);
        }

        for (Future<String> future : futureList) {
            future.get();
//                System.out.println(future.get());
        }
    }

 

输出

======-----------------------------并发:2019-08-06T21:11:21.970
======-----------------------------并发:2019-08-06T21:11:21.970
======-----------------------------并发:2019-08-06T21:11:21.970
======-----------------------------并发:2019-08-06T21:11:21.970
======-----------------------------并发:2019-08-06T21:11:21.970
======-----------------------------2019-08-06T21:11:22.488:pool-1-thread-2;守护线程:false
======-----------------------------2019-08-06T21:11:22.610:pool-1-thread-1;守护线程:false
======-----------------------------2019-08-06T21:11:22.635:pool-1-thread-4;守护线程:false
======-----------------------------2019-08-06T21:11:23.222:pool-1-thread-5;守护线程:false
======-----------------------------2019-08-06T21:11:23.807:pool-1-thread-3;守护线程:false
======-----------------------------并发:2019-08-06T21:11:25.161
======-----------------------------并发:2019-08-06T21:11:25.161
======-----------------------------并发:2019-08-06T21:11:25.161
======-----------------------------并发:2019-08-06T21:11:25.161
======-----------------------------并发:2019-08-06T21:11:25.161
======-----------------------------2019-08-06T21:11:26.123:pool-1-thread-2;守护线程:false
======-----------------------------2019-08-06T21:11:26.183:pool-1-thread-4;守护线程:false
======-----------------------------2019-08-06T21:11:26.295:pool-1-thread-3;守护线程:false
======-----------------------------2019-08-06T21:11:26.701:pool-1-thread-5;守护线程:false
======-----------------------------2019-08-06T21:11:27.016:pool-1-thread-1;守护线程:false
======-----------------------------并发:2019-08-06T21:11:28.752
======-----------------------------并发:2019-08-06T21:11:28.752
======-----------------------------并发:2019-08-06T21:11:28.752
======-----------------------------并发:2019-08-06T21:11:28.752
======-----------------------------并发:2019-08-06T21:11:28.752
======-----------------------------2019-08-06T21:11:28.983:pool-1-thread-3;守护线程:false
======-----------------------------2019-08-06T21:11:29.548:pool-1-thread-5;守护线程:false
======-----------------------------2019-08-06T21:11:29.668:pool-1-thread-1;守护线程:false
======-----------------------------2019-08-06T21:11:29.827:pool-1-thread-2;守护线程:false
======-----------------------------2019-08-06T21:11:29.868:pool-1-thread-4;守护线程:false

 

 

 

 

 

 

 

 

二、

006-多线程-JUC线程池-并发测试程序

标签:countdown   void   rup   pos   守护线程   线程   允许   java代码   rri   

原文地址:https://www.cnblogs.com/bjlhx/p/11302729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!