Răsfoiți Sursa

获取cpu核心数为并发数

wangliang 4 ani în urmă
părinte
comite
60995aeae2

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/OrgCenterDataDisposeService.java

@@ -11,6 +11,6 @@ import java.io.IOException;
  */
 public interface OrgCenterDataDisposeService {
 
-    @Async("arbitrateThreadPool")
+    @Async("taskThreadPool")
     void updateSchoolInfo() throws IOException;
 }

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/TBTaskService.java

@@ -145,7 +145,7 @@ public interface TBTaskService extends IService<TBTask> {
     /**
      * 更新任务状态
      */
-    @Async("arbitrateThreadPool")
+    @Async("taskThreadPool")
     public void updateStatus();
 
     /**

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/create/AsyncCreateTaskTemplete.java

@@ -30,7 +30,7 @@ public abstract class AsyncCreateTaskTemplete extends AsyncImportTaskTemplete {
      * @return
      * @throws IOException
      */
-    @Async("arbitrateThreadPool")
+    @Async("taskThreadPool")
     public Result createPdf(Map<String, Object> map, CallbackCreatePdf callbackCreatePdf) throws IOException {
         return null;
     }

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/export/AsyncExportTaskTemplete.java

@@ -51,7 +51,7 @@ public abstract class AsyncExportTaskTemplete {
      * @param map
      * @return
      */
-    @Async("arbitrateThreadPool")
+    @Async("taskThreadPool")
     public abstract Result exportTask(Map<String, Object> map) throws Exception;
 
     /**

+ 1 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/importData/AsyncImportTaskTemplete.java

@@ -54,7 +54,7 @@ public abstract class AsyncImportTaskTemplete {
      * @return
      * @throws IOException
      */
-    @Async("arbitrateThreadPool")
+    @Async("taskThreadPool")
     public abstract Result importTask(Map<String, Object> map) throws IOException, Exception;
 
     /**

+ 24 - 14
distributed-print-business/src/main/java/com/qmth/distributed/print/business/threadPool/MyThreadPool.java

@@ -1,6 +1,8 @@
 package com.qmth.distributed.print.business.threadPool;//package com.qmth.themis.business.threadPool;
 
 import com.qmth.distributed.print.common.contant.SystemConstant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -20,15 +22,17 @@ import java.util.concurrent.ThreadPoolExecutor;
  */
 @Configuration
 public class MyThreadPool extends ThreadPoolTaskExecutor {
+    private final static Logger log = LoggerFactory.getLogger(MyThreadPool.class);
 
-    public MyThreadPool arbitratePoolTaskExecutor = null;
+    public MyThreadPool threadPoolTaskExecutor = null;
+    static final int cpuNum = Runtime.getRuntime().availableProcessors();
 
     @Value("${sys.config.threadPoolCoreSize}")
     Integer threadPoolCoreSize;
 
     @PostConstruct
     public void init() {
-        arbitrateThreadPool();
+        taskThreadPool();
     }
 
     /**
@@ -37,20 +41,26 @@ public class MyThreadPool extends ThreadPoolTaskExecutor {
      * @return
      */
     @Bean
-    public Executor arbitrateThreadPool() {
-        if (Objects.isNull(arbitratePoolTaskExecutor)) {
-            arbitratePoolTaskExecutor = new MyThreadPool();
-            arbitratePoolTaskExecutor.setCorePoolSize(threadPoolCoreSize);//核心线程数
-            arbitratePoolTaskExecutor.setMaxPoolSize(threadPoolCoreSize);//最大线程数
-            arbitratePoolTaskExecutor.setKeepAliveSeconds(SystemConstant.THREAD_POOL_KEEP_ALIVE_SECONDS);//线程空闲时间
-            arbitratePoolTaskExecutor.setQueueCapacity(SystemConstant.THREAD_POOL_QUEUE_CAPACITY);//队列容量
-            arbitratePoolTaskExecutor.setThreadNamePrefix(SystemConstant.THREAD_POOL_NAME);
-
+    public Executor taskThreadPool() {
+        if (Objects.isNull(threadPoolTaskExecutor)) {
+            log.info("cpuNum:{}", cpuNum);
+            threadPoolTaskExecutor = new MyThreadPool();
+            if (cpuNum > 0) {
+                threadPoolTaskExecutor.setCorePoolSize(cpuNum);//核心线程数
+                threadPoolTaskExecutor.setMaxPoolSize(cpuNum * 2);//最大线程数
+            } else {
+                threadPoolTaskExecutor.setCorePoolSize(threadPoolCoreSize);//核心线程数
+                threadPoolTaskExecutor.setMaxPoolSize(threadPoolCoreSize * 2);//最大线程数
+            }
+            threadPoolTaskExecutor.setKeepAliveSeconds(SystemConstant.THREAD_POOL_KEEP_ALIVE_SECONDS);//线程空闲时间
+            threadPoolTaskExecutor.setQueueCapacity(SystemConstant.THREAD_POOL_QUEUE_CAPACITY);//队列容量
+            threadPoolTaskExecutor.setThreadNamePrefix(SystemConstant.THREAD_POOL_NAME);
+            threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);
             // rejection-policy:当pool已经达到max size的时候,如何处理新任务
             // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
-            arbitratePoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
-            arbitratePoolTaskExecutor.initialize();
+            threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+            threadPoolTaskExecutor.initialize();
         }
-        return arbitratePoolTaskExecutor;
+        return threadPoolTaskExecutor;
     }
 }

+ 2 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -43,7 +43,6 @@ public class PdfUtil {
         }
         Document document = null;
         PdfCopy copy = null;
-        PdfReader reader = null;
         StringJoiner dirName = null;
         try {
             if (Objects.nonNull(files) && files.length > 0) {
@@ -62,13 +61,14 @@ public class PdfUtil {
                 copy = new PdfSmartCopy(document, new FileOutputStream(outputPath));
                 document.open();
                 for (int i = 0; i < files.length; i++) {
-                    reader = new PdfReader(files[i]);
+                    PdfReader reader = new PdfReader(files[i]);
                     int numberOfPages = reader.getNumberOfPages();
                     for (int j = 1; j <= numberOfPages; j++) {
                         document.newPage();
                         PdfImportedPage page = copy.getImportedPage(reader, j);
                         copy.addPage(page);
                     }
+                    reader.close();
                 }
             }
         } catch (Exception e) {
@@ -77,9 +77,6 @@ public class PdfUtil {
             if (Objects.nonNull(document)) {
                 document.close();
             }
-            if (Objects.nonNull(reader)) {
-                reader.close();
-            }
             if (Objects.nonNull(copy)) {
                 copy.flush();
                 copy.close();

+ 1 - 1
distributed-print-common/src/main/java/com/qmth/distributed/print/common/contant/SystemConstant.java

@@ -125,7 +125,7 @@ public class SystemConstant {
     /**
      * 线程池配置
      */
-    public static final String THREAD_POOL_NAME = "arbitrateThreadPool";
+    public static final String THREAD_POOL_NAME = "taskThreadPool";
 //    public static final int THREAD_POOL_CORE_POOL_SIZE = 5;
 //    public static final int THREAD_POOL_MAX_POOL_SIZE = 100;
     public static final int THREAD_POOL_KEEP_ALIVE_SECONDS = 10;