ListeningExecutorService

1、初始化
ExecutorService newExecutor = new ThreadPoolExecutor(
poolSize,
poolSize,
120L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(poolSize * FACTOR),
r -> new Thread(r, "EXECUTOR_" + key), new AbortPolicy());
MoreExecutors.listeningDecorator(newExecutor);


2、ListeningExecutorService全部执行完才结束
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
try {
for (Callable<T> t : tasks) {
RunnableFuture<T> f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (int i = 0, size = futures.size(); i < size; i++) {
Future<T> f = futures.get(i);
if (!f.isDone()) {
try {
f.get();
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
}
}
}
done = true;
return futures;
} finally {
if (!done)
for (int i = 0, size = futures.size(); i < size; i++)
futures.get(i).cancel(true);
}
}
posted @ 2020-08-04 08:08  大米粒汪汪叫  阅读(1842)  评论(0编辑  收藏  举报