ソースを参照

init 使用 new ThreadPoolExecutor(...)

James 3 年 前
コミット
e43d31bbf3
1 ファイル変更16 行追加8 行削除
  1. 16 8
      src/main/java/com/jfinal/kit/ThreadPoolKit.java

+ 16 - 8
src/main/java/com/jfinal/kit/ThreadPoolKit.java

@@ -16,11 +16,7 @@
 
 package com.jfinal.kit;
 
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
 import com.jfinal.log.Log;
 
 /**
@@ -95,9 +91,21 @@ public class ThreadPoolKit {
 	 *
 	 * @param nThreads the number of threads in the pool
 	 */
-	public synchronized static void init(int nThreads) {
+	public static void init(int nThreads) {
+		init(nThreads, nThreads);
+	}
+
+	/**
+	 * 初始化
+	 *
+	 * @param corePoolSize the number of threads to keep in the pool,
+	 *                     even if they are idle, unless allowCoreThreadTimeOut is set
+	 * @param maximumPoolSize the maximum number of threads to allow in the pool
+	 */
+	public synchronized static void init(int corePoolSize, int maximumPoolSize) {
 		if (executor == null) {
-			executor = Executors.newFixedThreadPool(nThreads);
+			// executor = Executors.newFixedThreadPool(nThreads);
+			executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
 		} else {
 			Log.getLog(ThreadPoolKit.class).warn(ThreadPoolKit.class.getName() + " 已经初始化");
 		}
@@ -116,7 +124,7 @@ public class ThreadPoolKit {
 
 	public static ExecutorService getExecutor() {
 		if (executor == null) {
-			init(3);
+			init(5, 128);
 		}
 		return executor;
 	}