|
|
@ -22,20 +22,25 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private boolean isFastPool;
|
|
|
|
private boolean isFastPool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 是否自定义线程池
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private boolean isCustomPool;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 核心线程数量
|
|
|
|
* 核心线程数量
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Integer corePoolNum = calculateCoreNum();
|
|
|
|
private int corePoolSize = calculateCoreNum();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 最大线程数量
|
|
|
|
* 最大线程数量
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Integer maxPoolNum = corePoolNum + (corePoolNum >> 1);
|
|
|
|
private int maxPoolSize = corePoolSize + (corePoolSize >> 1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 线程存活时间
|
|
|
|
* 线程存活时间
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Long keepAliveTime = 30000L;
|
|
|
|
private long keepAliveTime = 30000L;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 线程存活时间单位
|
|
|
|
* 线程存活时间单位
|
|
|
@ -45,7 +50,7 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 队列最大容量
|
|
|
|
* 队列最大容量
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Integer capacity = 512;
|
|
|
|
private int capacity = 512;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 队列类型枚举
|
|
|
|
* 队列类型枚举
|
|
|
@ -87,6 +92,11 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder isCustomPool(Boolean isCustomPool) {
|
|
|
|
|
|
|
|
this.isCustomPool = isCustomPool;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder threadFactory(String threadNamePrefix) {
|
|
|
|
public ThreadPoolBuilder threadFactory(String threadNamePrefix) {
|
|
|
|
this.threadNamePrefix = threadNamePrefix;
|
|
|
|
this.threadNamePrefix = threadNamePrefix;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
@ -98,23 +108,23 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder corePoolNum(Integer corePoolNum) {
|
|
|
|
public ThreadPoolBuilder corePoolSize(int corePoolSize) {
|
|
|
|
this.corePoolNum = corePoolNum;
|
|
|
|
this.corePoolSize = corePoolSize;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder maxPoolNum(Integer maxPoolNum) {
|
|
|
|
public ThreadPoolBuilder maxPoolNum(int maxPoolSize) {
|
|
|
|
this.maxPoolNum = maxPoolNum;
|
|
|
|
this.maxPoolSize = maxPoolSize;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder poolThreadNum(Integer corePoolNum, Integer maxPoolNum) {
|
|
|
|
public ThreadPoolBuilder poolThreadSize(int corePoolSize, int maxPoolSize) {
|
|
|
|
this.corePoolNum = corePoolNum;
|
|
|
|
this.corePoolSize = corePoolSize;
|
|
|
|
this.maxPoolNum = maxPoolNum;
|
|
|
|
this.maxPoolSize = maxPoolSize;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder keepAliveTime(Long keepAliveTime) {
|
|
|
|
public ThreadPoolBuilder keepAliveTime(long keepAliveTime) {
|
|
|
|
this.keepAliveTime = keepAliveTime;
|
|
|
|
this.keepAliveTime = keepAliveTime;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -124,18 +134,18 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder keepAliveTime(Long keepAliveTime, TimeUnit timeUnit) {
|
|
|
|
public ThreadPoolBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
|
|
|
|
this.keepAliveTime = keepAliveTime;
|
|
|
|
this.keepAliveTime = keepAliveTime;
|
|
|
|
this.timeUnit = timeUnit;
|
|
|
|
this.timeUnit = timeUnit;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder capacity(Integer capacity) {
|
|
|
|
public ThreadPoolBuilder capacity(int capacity) {
|
|
|
|
this.capacity = capacity;
|
|
|
|
this.capacity = capacity;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder workQueue(QueueTypeEnum queueType, Integer capacity) {
|
|
|
|
public ThreadPoolBuilder workQueue(QueueTypeEnum queueType, int capacity) {
|
|
|
|
this.queueType = queueType;
|
|
|
|
this.queueType = queueType;
|
|
|
|
this.capacity = capacity;
|
|
|
|
this.capacity = capacity;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
@ -146,17 +156,16 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 使用此方式赋值 workQueue, capacity 失效
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param queueType
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public ThreadPoolBuilder workQueue(QueueTypeEnum queueType) {
|
|
|
|
public ThreadPoolBuilder workQueue(QueueTypeEnum queueType) {
|
|
|
|
this.queueType = queueType;
|
|
|
|
this.queueType = queueType;
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ThreadPoolBuilder workQueue(BlockingQueue workQueue) {
|
|
|
|
|
|
|
|
this.workQueue = workQueue;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 构建
|
|
|
|
* 构建
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -164,6 +173,9 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ThreadPoolExecutor build() {
|
|
|
|
public ThreadPoolExecutor build() {
|
|
|
|
|
|
|
|
if (isCustomPool) {
|
|
|
|
|
|
|
|
return buildCustomPool(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
return isFastPool ? buildFastPool(this) : buildPool(this);
|
|
|
|
return isFastPool ? buildFastPool(this) : buildPool(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -196,6 +208,16 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
return AbstractBuildThreadPoolTemplate.buildFastPool(buildInitParam(builder));
|
|
|
|
return AbstractBuildThreadPoolTemplate.buildFastPool(buildInitParam(builder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构建自定义线程池
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param builder
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static ThreadPoolExecutor buildCustomPool(ThreadPoolBuilder builder) {
|
|
|
|
|
|
|
|
return AbstractBuildThreadPoolTemplate.buildCustomPool(buildInitParam(builder));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 构建初始化参数
|
|
|
|
* 构建初始化参数
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -207,8 +229,8 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
AbstractBuildThreadPoolTemplate.ThreadPoolInitParam initParam =
|
|
|
|
AbstractBuildThreadPoolTemplate.ThreadPoolInitParam initParam =
|
|
|
|
new AbstractBuildThreadPoolTemplate.ThreadPoolInitParam(builder.threadNamePrefix, builder.isDaemon);
|
|
|
|
new AbstractBuildThreadPoolTemplate.ThreadPoolInitParam(builder.threadNamePrefix, builder.isDaemon);
|
|
|
|
|
|
|
|
|
|
|
|
initParam.setCorePoolNum(builder.corePoolNum)
|
|
|
|
initParam.setCorePoolNum(builder.corePoolSize)
|
|
|
|
.setMaxPoolNum(builder.maxPoolNum)
|
|
|
|
.setMaxPoolNum(builder.maxPoolSize)
|
|
|
|
.setKeepAliveTime(builder.keepAliveTime)
|
|
|
|
.setKeepAliveTime(builder.keepAliveTime)
|
|
|
|
.setCapacity(builder.capacity)
|
|
|
|
.setCapacity(builder.capacity)
|
|
|
|
.setRejectedExecutionHandler(builder.rejectedExecutionHandler)
|
|
|
|
.setRejectedExecutionHandler(builder.rejectedExecutionHandler)
|
|
|
@ -216,11 +238,10 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
|
|
|
|
|
|
|
|
|
|
|
|
// 快速消费线程池内置指定线程池
|
|
|
|
// 快速消费线程池内置指定线程池
|
|
|
|
if (!builder.isFastPool) {
|
|
|
|
if (!builder.isFastPool) {
|
|
|
|
BlockingQueue blockingQueue = BlockingQueueUtil.createBlockingQueue(builder.queueType.type, builder.capacity);
|
|
|
|
if (builder.workQueue == null) {
|
|
|
|
if (blockingQueue == null) {
|
|
|
|
builder.workQueue = BlockingQueueUtil.createBlockingQueue(builder.queueType.type, builder.capacity);
|
|
|
|
blockingQueue = builder.workQueue;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initParam.setWorkQueue(blockingQueue);
|
|
|
|
initParam.setWorkQueue(builder.workQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return initParam;
|
|
|
|
return initParam;
|
|
|
|