threadpoolutil.java
来自「可以实现P2P聊天通信」· Java 代码 · 共 30 行
JAVA
30 行
package jxtamessenger.util;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class ThreadPoolUtil {
private static final Logger LOG = Logger.getLogger(ThreadPoolUtil.class.getName());
private static final int WAIT_EXISTING_TASKS_TERMINATE = 1;
private static final int WAIT_TASKS_RESPOND_CANCELLED = 1;
public static void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(WAIT_EXISTING_TASKS_TERMINATE, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(WAIT_TASKS_RESPOND_CANCELLED, TimeUnit.SECONDS))
LOG.warning("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?