多线程并发测试,用了都说好
作者:互联网
@Test
public void testMultiThread() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(10);
ExecutorService executorService = Executors.newFixedThreadPool(10);
CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
for (int i = 0; i < 10; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(60)*1000);
cyclicBarrier.await();
// 业务方法
tagContentServiceTask.taskContentAlreadyQueue();
log.info("开始执行 {},{}",Thread.currentThread().getName());
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
countDownLatch.await();
executorService.shutdown();
}
标签:cyclicBarrier,10,说好,CountDownLatch,并发,executorService,countDownLatch,new,多线程 来源: https://www.cnblogs.com/lyc88/p/15748462.html