⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadpoolutil.java

📁 可以实现P2P聊天通信
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -