5 changed files with 192 additions and 159 deletions
@ -0,0 +1,48 @@
|
||||
package com.mh.user.utils; |
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue; |
||||
import java.util.concurrent.ThreadPoolExecutor; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* @author LJF |
||||
* @title : 单例的线程池 |
||||
* @description 使用静态内部类进行创建,专门解析接收到的报文数据 |
||||
* @updateTime 2020-12-09 |
||||
* @throws : |
||||
*/ |
||||
public class ComThreadPoolService { |
||||
|
||||
/** 线程池保持ALIVE状态线程数 */ |
||||
public static final int CORE_POOL_SIZE = 10; |
||||
|
||||
/** 线程池最大线程数 */ |
||||
public static final int MAX_POOL_SIZE = 50; |
||||
|
||||
/** 空闲线程回收时间 */ |
||||
public static final int KEEP_ALIVE_TIME = 30000; |
||||
|
||||
/** 线程池等待队列 */ |
||||
public static final int BLOCKING_QUEUE_SIZE = 1000; |
||||
|
||||
// 私有化构造器
|
||||
private ComThreadPoolService(){} |
||||
|
||||
// 对外访问的公共方法
|
||||
public static ThreadPoolExecutor getInstance() { |
||||
return ThreadPoolServiceHolder.instance; |
||||
} |
||||
|
||||
//写一个静态内部类,里面实例化外部类
|
||||
private static class ThreadPoolServiceHolder { |
||||
private static final ThreadPoolExecutor instance = new ThreadPoolExecutor( |
||||
CORE_POOL_SIZE, // 线程池保持存活的线程数
|
||||
MAX_POOL_SIZE, // 最大线程数
|
||||
KEEP_ALIVE_TIME, // 空闲线程回收时间
|
||||
TimeUnit.MICROSECONDS, // 单位
|
||||
new LinkedBlockingQueue<>(BLOCKING_QUEUE_SIZE), // 线程队列
|
||||
new ThreadPoolExecutor.AbortPolicy() // 线程池对拒绝任务的处理策略
|
||||
); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue