From 301115d5e96b56cd093eee3fcff2d60a15184162 Mon Sep 17 00:00:00 2001
From: hjg <hjg@123.com>
Date: 星期一, 05 二月 2024 14:28:29 +0800
Subject: [PATCH] 明晟装车记录

---
 JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java |   64 +++++++++++++++++++++-----------
 1 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java b/JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java
index 43f01ff..4008200 100644
--- a/JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java
+++ b/JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java
@@ -1,7 +1,7 @@
 package com.mandi.fendan.util;
 
 import javax.validation.constraints.NotNull;
-import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
@@ -27,25 +27,27 @@
     //瀛樻椿鏃堕棿
     private long keepAliveTime = 1;
     private TimeUnit unit = TimeUnit.HOURS;
-    private ThreadPoolExecutor executor;
+//    private ThreadPoolExecutor executor;
 
+    ScheduledThreadPoolExecutor executor;
     private ThreadPoolUtil() {
         //缁檆orePoolSize璧嬪�硷細褰撳墠璁惧鍙敤澶勭悊鍣ㄦ牳蹇冩暟*2 + 1,鑳藉璁ヽpu鐨勬晥鐜囧緱鍒版渶澶х▼搴︽墽琛岋紙鏈夌爺绌惰璇佺殑锛�
         corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1;
         maxPoolSize = corePoolSize;
-        executor = new ThreadPoolExecutor(
-                //褰撴煇涓牳蹇冧换鍔℃墽琛屽畬姣曪紝浼氫緷娆′粠缂撳啿闃熷垪涓彇鍑虹瓑寰呬换鍔�
-                corePoolSize,
-                // 鐒跺悗new LinkedBlockingQueue(),鐒跺悗maximumPoolSize,浣嗘槸瀹冪殑鏁伴噺鏄寘鍚簡corePoolSize鐨�
-                maxPoolSize,
-                //琛ㄧず鐨勬槸maximumPoolSize褰撲腑绛夊緟浠诲姟鐨勫瓨娲绘椂闂�
-                keepAliveTime,
-                unit,
-                //缂撳啿闃熷垪锛岀敤浜庡瓨鏀剧瓑寰呬换鍔★紝Linked鐨勫厛杩涘厛鍑�
-                new LinkedBlockingQueue(),
-                new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
-                new ThreadPoolExecutor.AbortPolicy()
-        );
+//        executor = new ThreadPoolExecutor(
+//                //褰撴煇涓牳蹇冧换鍔℃墽琛屽畬姣曪紝浼氫緷娆′粠缂撳啿闃熷垪涓彇鍑虹瓑寰呬换鍔�
+//                corePoolSize,
+//                // 鐒跺悗new LinkedBlockingQueue(),鐒跺悗maximumPoolSize,浣嗘槸瀹冪殑鏁伴噺鏄寘鍚簡corePoolSize鐨�
+//                maxPoolSize,
+//                //琛ㄧず鐨勬槸maximumPoolSize褰撲腑绛夊緟浠诲姟鐨勫瓨娲绘椂闂�
+//                keepAliveTime,
+//                unit,
+//                //缂撳啿闃熷垪锛岀敤浜庡瓨鏀剧瓑寰呬换鍔★紝Linked鐨勫厛杩涘厛鍑�
+//                new LinkedBlockingQueue(),
+//                new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
+//                new ThreadPoolExecutor.AbortPolicy()
+//        );
+          executor =  new ScheduledThreadPoolExecutor(corePoolSize);
     }
 
     public static ThreadPoolUtil getInstance() {
@@ -65,18 +67,36 @@
      * @param runnable
      */
     public void execute(Runnable runnable) {
+        checkNull();
+        if (runnable != null) {
+            executor.execute(runnable);
+        }
+    }
+
+    void checkNull() {
         if (executor == null) {
-            executor = new ThreadPoolExecutor(
-                    corePoolSize,
-                    maxPoolSize,
-                    keepAliveTime,
-                    TimeUnit.SECONDS,
-                    new LinkedBlockingQueue(),
+//            executor = new ThreadPoolExecutor(
+//                    corePoolSize,
+//                    maxPoolSize,
+//                    keepAliveTime,
+//                    TimeUnit.SECONDS,
+//                    new LinkedBlockingQueue(),
+//                    new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
+//                    new ThreadPoolExecutor.AbortPolicy());
+            executor =  new ScheduledThreadPoolExecutor(corePoolSize,
                     new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
                     new ThreadPoolExecutor.AbortPolicy());
         }
+    }
+    /**
+     *
+     * @param runnable
+     * @param delay 寤惰繜鎵ц锛屽崟浣嶆绉�
+     */
+    public void execute(Runnable runnable,long delay) {
+        checkNull();
         if (runnable != null) {
-            executor.execute(runnable);
+            executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
         }
     }
 

--
Gitblit v1.9.3