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  List> invokeAll(Collection<? extends Callable> tasks)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
ArrayList> futures = new ArrayList>(tasks.size());
boolean done = false;
try {
for (Callable t : tasks) {
RunnableFuture f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (int i = 0, size = futures.size(); i < size; i++) {
Future 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);
}
}

相关