@Test public void CountDownLatchTest3() throws InterruptedException { // 并发线程数 int count = 100; final CountDownLatch latch = new CountDownLatch(count); for (int i = 0;i < count; i++) { Thread t = new Thread(new Runnable() { @Override public void run() { String name = Thread.currentThread().getName(); try { System.out.println("现场名称:"+ name); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }finally { latch.countDown(); } System.out.println("线程:" + name + " 执行完毕!"); } }); t.setName("testCountDownLatchTreadName"+i); t.start(); } System.out.println("等待任务执行完毕" + new Date()); latch.await(); System.out.println("全部任务已经执行完毕" + new Date()); Assert.assertTrue(true); }
CountDownLatch使用示例
于 2022-12-07 09:26:26 首次发布