diff --git a/src/main/java/com/imageServer/context/ShutdownResources.java b/src/main/java/com/imageServer/context/ShutdownResources.java index a72db9b..3524182 100644 --- a/src/main/java/com/imageServer/context/ShutdownResources.java +++ b/src/main/java/com/imageServer/context/ShutdownResources.java @@ -29,7 +29,7 @@ public void contextDestroyed(ServletContextEvent servletContextEvent) { log.info("监听处理处理web容器关闭事件"); log.info("关闭线程池"); - ExecutorFactory.getExecutor().shutdown(); + ExecutorFactory.EXECUTE.getExecutor().shutdown(); } } diff --git a/src/main/java/com/imageServer/factory/ExecutorFactory.java b/src/main/java/com/imageServer/factory/ExecutorFactory.java index 790bc90..9c41d85 100644 --- a/src/main/java/com/imageServer/factory/ExecutorFactory.java +++ b/src/main/java/com/imageServer/factory/ExecutorFactory.java @@ -8,18 +8,31 @@ /** * 并发线程执行对象工厂方法 + * 单例 * Copyright 2014-2015 the original ql * Created by QianLong on 15-1-23. */ -public class ExecutorFactory { +public enum ExecutorFactory { - private static ExecutorService executorService = null; + EXECUTE; + private ExecutorService executorService; + + ExecutorFactory(){ + if(executorService == null){ + final int cpuCore = Runtime.getRuntime().availableProcessors(); + //线程数(当前CPU数+1) + final int poolSize = cpuCore+1; + //定义并发执行服务 + executorService = Executors.newFixedThreadPool(poolSize); + + } + } /** * 获取执行类 * @return */ - public static ExecutorService getExecutor(){ + public ExecutorService getExecutor(){ if(executorService == null){ final int cpuCore = Runtime.getRuntime().availableProcessors(); //线程数(当前CPU数+1) @@ -31,6 +44,4 @@ public static ExecutorService getExecutor(){ return executorService; } - private ExecutorFactory(){} - } diff --git a/src/main/java/com/imageServer/util/ExecuteThreadUtil.java b/src/main/java/com/imageServer/util/ExecuteThreadUtil.java index e3ec55c..cea9421 100644 --- a/src/main/java/com/imageServer/util/ExecuteThreadUtil.java +++ b/src/main/java/com/imageServer/util/ExecuteThreadUtil.java @@ -15,7 +15,7 @@ */ public class ExecuteThreadUtil { - private static ExecutorService executorService = ExecutorFactory.getExecutor(); + private static ExecutorService executorService = ExecutorFactory.EXECUTE.getExecutor(); /** * 执行一个无返回值的线程任务