Browse Source

!76 fix: 高核服务器会出现核心线程数量会大于非核心线程的bug

Merge pull request !76 from blue/v_2.0.0
master
blue 2 years ago
committed by Gitee
parent
commit
eb3cdae0c9
No known key found for this signature in database GPG Key ID: 173E9B9CA92EEF8F
  1. 29
      im-commom/src/main/java/com/bx/imcommon/util/ThreadPoolExecutorFactory.java

29
im-commom/src/main/java/com/bx/imcommon/util/ThreadPoolExecutorFactory.java

@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit;
/** /**
* 创建单例线程池 * 创建单例线程池
*
* @author Andrews * @author Andrews
* @date 2023/11/30 11:12 * @date 2023/11/30 11:12
*/ */
@ -19,7 +20,8 @@ public final class ThreadPoolExecutorFactory {
* CPU 密集型核心线程数 = CPU核数 + 1 * CPU 密集型核心线程数 = CPU核数 + 1
* IO 密集型核心线程数 = CPU核数 * 2 * IO 密集型核心线程数 = CPU核数 * 2
*/ */
private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2; private static final int CORE_POOL_SIZE =
Math.min(ThreadPoolExecutorFactory.MAX_IMUM_POOL_SIZE, Runtime.getRuntime().availableProcessors() * 2);
/** /**
* maximumPoolSize - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用) * maximumPoolSize - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用)
*/ */
@ -63,19 +65,18 @@ public final class ThreadPoolExecutorFactory {
synchronized (ThreadPoolExecutorFactory.class) { synchronized (ThreadPoolExecutorFactory.class) {
if (null == threadPoolExecutor) { if (null == threadPoolExecutor) {
threadPoolExecutor = new ThreadPoolExecutor( threadPoolExecutor = new ThreadPoolExecutor(
//核心线程数 //核心线程数
CORE_POOL_SIZE, CORE_POOL_SIZE,
//最大线程数,包含临时线程 //最大线程数,包含临时线程
MAX_IMUM_POOL_SIZE, MAX_IMUM_POOL_SIZE,
//临时线程的存活时间 //临时线程的存活时间
KEEP_ALIVE_TIME, KEEP_ALIVE_TIME,
//时间单位(毫秒) //时间单位(毫秒)
TimeUnit.MILLISECONDS, TimeUnit.MILLISECONDS,
//等待队列 //等待队列
new LinkedBlockingQueue<>(QUEUE_SIZE), new LinkedBlockingQueue<>(QUEUE_SIZE),
//拒绝策略 //拒绝策略
new ThreadPoolExecutor.CallerRunsPolicy() new ThreadPoolExecutor.CallerRunsPolicy());
);
} }
} }
} }

Loading…
Cancel
Save