所以,我有一个用于关闭 Java 中的执行程序服务的代码,如下 -
public void shutdownExecutor() {
executor.shutdown();
try {
executor.awaitTermination(requestInfo.getUploadThreadCount() * 30,
TimeUnit.SECONDS);
} catch (InterruptedException e) {
logger.error(e);
}
}
在上面的代码中,正如您所看到的,客户端应用程序(控制台或 Web)会变为空白,直到线程计数 * 30 秒用完。如何在此处显示友好消息?
最佳答案
如果您想在等待时显示定期消息,您可以这样做(例如,每 1 秒显示一条消息,最多 30 秒):
try {
long endTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
while (true) {
long timeout = Math.min(
TimeUnit.SECONDS.toNanos(1), endTime - System.nanoTime());
if (timeout <= 0
|| executor.awaitTermination(timeout, TimeUnit.NANOSECONDS)) {
break;
}
System.err.println("Still waiting for timeout");
}
} catch (InterruptedException e) {
logger.error(e);
Thread.currentThread().interrupt();
}
关于java - Java中ExecutorService关闭时显示友好消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754755/