当您SimpleTimeLimiter
使用默认构造函数创建时 - 他创建Executors.newCachedThreadPool()
了您无法控制的自己,因此您的应用程序将完成所有线程。来自 Javadoc
... 60 秒内未使用的线程将被终止并从缓存中删除....
如果您创建自己的 ExecutorService 并使用此 executorService 创建 SimpleTimeLimiter,那么您可以在关闭挂钩上关闭 executorService。
private final ExecutorService executor;
private final TimeLimiter timeLimiter;
...
executor = Executors.newCachedThreadPool();
timeLimiter = new SimpleTimeLimiter(executor);
...
public void shutdown() {
if (executor == null || executor.isShutdown()) {
return;
}
executor.shutdown();
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.log(Level.WARNING, "Interrupted during executor termination.", e);
Thread.currentThread().interrupt();
}
executor.shutdownNow();
}