java并发测试帮助方法
作者:互联网
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,doneSignal,CountDownLatch,并发,测试,countDownLatch,action,new,public 来源: https://www.cnblogs.com/warrior/p/12204129.html