public class Main {
public static void main(String[] args) throws Exception{
final int count = 100 ;
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(count);
for (int i=0; i<100; i++){
final int j = i;
executorService.execute(() ->{
try {
Thread.sleep(100);
System.out.println(j);
} catch (Exception e){
} finally{
countDownLatch.countDown();
}
});
}
countDownLatch.await();//等待所有线程完成
//countDownLatch.await(10,TimeUnit.MICROSECONDS); //等待10ms
System.out.println("finish");
executorService.shutdown();
}
}
CountDownLatch学习笔记
最新推荐文章于 2021-10-10 12:45:12 发布