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

java并发测试帮助方法

时间:2020-01-17 09:21:30      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:并发测试   latch   完成   art   get   测试   enc   帮助   require   

public class SimulateHighConcurrency {

    public static void run(int num, Consumer<Thread> action) {
        Objects.requireNonNull(action);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < num; i++) {
            new Thread(() -> {
                try {
                    action.accept(Thread.currentThread());
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        //线程创建完成之后同时启动
        countDownLatch.countDown();
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch doneSignal = new CountDownLatch(2000);
        SimulateHighConcurrency.run(2000, (t) -> {
            System.out.println(t.getName());
            doneSignal.countDown();
        });
        doneSignal.await();
        System.out.println("done");
    }

}

java并发测试帮助方法

标签:并发测试   latch   完成   art   get   测试   enc   帮助   require   

原文地址:https://www.cnblogs.com/warrior/p/12204129.html

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