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"); } }
相关推荐
ErixHao 2020-05-30
ALiDan 2020-04-16
王道革 2020-04-11
sunzxh 2020-03-23
付春杰Blog 2020-02-17
spb 2020-02-01
xzkjgw 2020-01-17
Cheetahcubs 2020-01-04
zhujiangtaotaise 2019-12-26
zhangxiaocc 2019-11-07
Wisher 2017-06-07
思捻如枫 2019-07-01
leys 2019-07-01
讨厌什么变成什么 2019-06-30
浪里xiao白龙 2019-06-28
mojianc 2019-06-28
liwenbocsu 2019-06-28
了缺 2019-06-28
helowken 2019-06-25