正文:
版本一:
public void manyThread() {
List list = Arrays.asList("1", "2", "3");
// 计数器
CountDownLatch downLatch = new CountDownLatch(list.size());
// 创建线程
ExecutorService executorService = null;
try {
executorService = Executors.newFixedThreadPool(list.size());
for (String item : list) {
// 开启线程
executorService.execute(() -> {
try {
// 业务处理
System.out.println(item);
} finally {
downLatch.countDown();
}
});
}
downLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (executorService != null) {
executorService.shutdown();
}
}
}



