###开发中,避免各种各样的实现,统一线程池实现
- 线程池执行器
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class DefaultThreadPoolExecutor {
private ThreadPoolExecutor pool = null;
public DefaultThreadPoolExecutor() {
this(1, 10, 30, 100);
}
/**
* 初始化线程
*
* @param corePoolSize 核心线程池大小,最小数
* @param maximumPoolSize 最大线程池大小
* @param keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间,单位分钟
* @param queueCapacity 队列容量
*/
public DefaultThreadPoolExecutor(Integer corePoolSize, Integer maximumPoolSize, Integer keepAliveTime, Integer queueCapacity) {
pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(queueCapacity), new DefaultThreadFactory(), new DefaultRejectedExecutionHandler());
log.info("初始化线程池:{corePoolSize:{},maximumPoolSize:{},keepAliveTime:{},queueCapacity:{}}", corePoolSize, maximumPoolSize, keepAliveTime, queueCapacity);
}
public void destory() {
if (pool != null) {
pool.shutdownNow();
}
}
public ExecutorService getDefaultThreadPoolExecutor() {
return this.pool;
}
private class DefaultRejectedExecutionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
try {
CommonTask commonTask = (CommonTask) runnable;
log.info("线程池添加任务{}被拒绝,触发拒绝策略", commonTask.getTaskName());
//todo Inserts the specified element into this queue, waiting if necessary
// for space to become available.
executor.getQueue()
.put(runnable);
} catch (InterruptedException e) {
log.error("在堵塞等待方式添加任务到线程时获得异常", e);
}
}
}
/**
* 线程工厂,指定线程名
*/
private class DefaultThreadFactory implements ThreadFactory {
private AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
String threadName = DefaultThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);
thread.setName(threadName);
return thread;
}
}
}
- 线程池管理器
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@ConditionalOnProperty(name = "thread.active", havingValue = "true")
@Slf4j
public class ThreadPoolManager {
@Value("${thread.pool.coreSize}")
private Integer poolCoreSize;
@Value("${thread.pool.maxSize}")
private Integer poolMaxSize;
@Value("${thread.pool.keepAliveTime}")
private Integer poolKeepAliveTime;
@Value("${thread.queue.capacity}")
private Integer queueCapacity;
private static ThreadPoolManager instance;
private DefaultThreadPoolExecutor threadPoolExecutor;
@PostConstruct
public void init() {
instance = this;
}
private DefaultThreadPoolExecutor getThreadPoolExecutor() {
if (threadPoolExecutor == null) {
synchronized (this) {
threadPoolExecutor = new DefaultThreadPoolExecutor(poolCoreSize, poolMaxSize, poolKeepAliveTime, queueCapacity);
}
}
return threadPoolExecutor;
}
public static ThreadPoolManager getInstance() {
return instance;
}
public void submit(CommonTask task) {
if (task != null) {
if (log.isDebugEnabled()) {
log.debug("新线程池任务:{}", task.getTaskName());
}
this.getThreadPoolExecutor()
.getDefaultThreadPoolExecutor()
.execute(task);
}
}
}
- 统一任务接口
public interface CommonTask extends Runnable {
String getTaskName();
}