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");
    }

}

相关文章:

  • 2021-05-30
  • 2021-07-28
  • 2022-02-10
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-15
  • 2021-09-20
  • 2022-01-13
  • 2022-12-23
  • 2021-11-27
  • 2022-03-04
  • 2021-12-09
相关资源
相似解决方案