方法一
ArrayList<Integer> integers = new ArrayList<>();
ThreadPoolExecutor executor = ThreadPoolUtils.init();
for (int i = 0; i < 10; i++) {
executor.execute(new Add(i, integers));
}
if(!executor.isShutdown()){
executor.shutdown();
}
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
}
System.out.println(integers+"----------------");
方法二
CountDownLatch latch = new CountDownLatch(fileList.size());
int processors = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = ThreadPoolUtils.init(processors*2, processors*5, 10L, TimeUnit.SECONDS,
fileList.size());
for (int i = 0; i < fileList.size(); i++) {
int finalI = i;
executor.execute(() -> {
do();
latch.countDown();
});
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!executor.isShutdown()){
executor.shutdown();
}