You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
46 lines
1.5 KiB
package com.mh.user.utils; |
|
|
|
import java.util.concurrent.*; |
|
|
|
/** |
|
* @author ljf |
|
* @title : 单例的线程池 |
|
* @description : 使用静态内部类进行创建,专门解析接收到的报文数据 |
|
* @updateTime 2020-12-09 |
|
* @throws : |
|
*/ |
|
public class ThreadPoolService { |
|
|
|
/** 线程池保持ALIVE状态线程数 */ |
|
public static final int CORE_POOL_SIZE = 5; |
|
|
|
/** 线程池最大线程数 */ |
|
public static final int MAX_POOL_SIZE = 10; |
|
|
|
/** 空闲线程回收时间 */ |
|
public static final int KEEP_ALIVE_TIME = 30000; |
|
|
|
/** 线程池等待队列 */ |
|
public static final int BLOCKING_QUEUE_SIZE = 100; |
|
|
|
// 私有化构造器 |
|
private ThreadPoolService(){} |
|
|
|
// 对外访问的公共方法 |
|
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() // 线程池对拒绝任务的处理策略 |
|
); |
|
} |
|
|
|
}
|
|
|