import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolUtils {
// 整个应用程序只创建一个线程池
private static ExecutorService threadPool = Executors.newCachedThreadPool();
/**
* 执行Runnable任务
*
* @param command
*/
public static void execute(Runnable command) {
if (threadPool.isShutdown()) {
// 如果线程池关闭,那么就再创建一个线程池
threadPool = Executors.newCachedThreadPool();
execute(command);
} else {
threadPool.execute(command);
}
}
/**
* 关闭线程池
*/
public static void shutdown() {
if (!threadPool.isShutdown()) {
threadPool.shutdown();
}
}
}
线程池工具类
最新推荐文章于 2024-05-03 17:39:16 发布