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

一、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:全部结束

可以看到 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

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

二、

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

时间: 2024-11-13 07:52:12

006-多线程-JUC线程池-并发测试程序的相关文章

java多线程系类:JUC线程池:03之线程池原理(二)(转)

概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代码(基于JDK1.7.0_40)线程池源码分析(一) 创建"线程池"(二) 添加任务到"线程池"(三) 关闭"线程池" 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509954.h

Java - &quot;JUC线程池&quot; 架构

Java多线程系列--"JUC线程池"01之 线程池架构 概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容--线程池.内容包括:线程池架构图线程池示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509903.html 线程池架构图 线程池的架构图如下: 1. Executor 它是"执行者"接口,

Java - &quot;JUC线程池&quot; ThreadPoolExecutor原理解析

Java多线程系列--"JUC线程池"02之 线程池原理(一) ThreadPoolExecutor简介 ThreadPoolExecutor是线程池类.对于线程池,可以通俗的将它理解为"存放一定数量线程的一个线程集合.线程池允许若个线程同时允许,允许同时运行的线程数量就是线程池的容量:当添加的到线程池中的线程超过它的容量时,会有一部分线程阻塞等待.线程池会通过相应的调度策略和拒绝策略,对添加到线程池中的线程进行管理." ThreadPoolExecutor数据结构

Java - &quot;JUC线程池&quot; Callable与Future

Java多线程系列--"JUC线程池"06之 Callable和Future Callable 和 Future 简介 Callable 和 Future 是比较有趣的一对组合.当我们需要获取线程的执行结果时,就需要用到它们.Callable用于产生结果,Future用于获取结果. 1. Callable Callable 是一个接口,它只包含一个call()方法.Callable是一个返回结果并且可能抛出异常的任务. 为了便于理解,我们可以将Callable比作一个Runnable接

Java - &quot;JUC线程池&quot; 线程状态与拒绝策略源码分析

Java多线程系列--"JUC线程池"04之 线程池原理(三) 本章介绍线程池的生命周期.在"Java多线程系列--"基础篇"01之 基本概念"中,我们介绍过,线程有5种状态:新建状态,就绪状态,运行状态,阻塞状态,死亡状态.线程池也有5种状态:然而,线程池不同于线程,线程池的5种状态是:Running, SHUTDOWN, STOP, TIDYING, TERMINATED. 线程池状态定义代码如下: private final AtomicI

多线程及线程池学习心得

一.线程的应用与特点 多线程是程序员不可或缺的技术能力,多线程技术在各个方面都有应用,特别在性能优化上更是起到至关重要的作用.但是,如果多线程写得不好,往往会适得其反,特别是高并发时会造成阻塞.超时等现象.多线程具有以下特点:1.独立性,拥有自己独立的资源,拥有自己私有的地址空间:2.动态性,进程具有自己的生命周期和各种不同的状态:3.并发性,多个进程可以在单个处理器上并发执行,不会相互影响,并行是指同一时刻有多条指令在多个处理器上同时执行.线程是进程的组成部分,一个进程可以拥有多个线程,一个线

C#多线程之线程池篇2

在上一篇C#多线程之线程池篇1中,我们主要学习了如何在线程池中调用委托以及如何在线程池中执行异步操作,在这篇中,我们将学习线程池和并行度.实现取消选项的相关知识. 三.线程池和并行度 在这一小节中,我们将学习对于大量的异步操作,使用线程池和分别使用单独的线程在性能上有什么差异性.具体操作步骤如下: 1.使用Visual Studio 2015创建一个新的控制台应用程序. 2.双击打开"Program.cs"文件,编写代码如下所示: 1 using System; 2 using Sys

C#多线程之线程池篇3

在上一篇C#多线程之线程池篇2中,我们主要学习了线程池和并行度以及如何实现取消选项的相关知识.在这一篇中,我们主要学习如何使用等待句柄和超时.使用计时器和使用BackgroundWorker组件的相关知识. 五.使用等待句柄和超时 在这一小节中,我们将学习如何在线程池中实现超时和正确地实现等待.具体操作步骤如下: 1.使用Visual Studio 2015创建一个新的控制台应用程序. 2.双击打开"Program.cs"文件,编写代码如下所示: 1 using System; 2 u

【JUC】JUC线程池框架综述

一.前言 在分析完了JUC的锁和集合框架后,下面进入JUC线程池框架的分析,下面给出JUC线程池的总体框架,之后再逐一进行分析. 二.JUC线程池框架图 说明:从上图可知,JUC线程池框架中的其他接口或类都直接或间接的继承了Executor接口,虽然Executors与其他类或者接口没有明显的关系,但是Executors是线程池的工具类,利用它可以生成各种线程池. 三.具体说明 3.1 Executors Executors是一个工具类,用其可以创建ExecutorService.Schedul