我们在工作中常常使用到线程池,为了方便配置和使用,我们一般写一个配置类来进行统一管理
我们使用 @Configuration 注解 ,让他被spring容器进行管理 ,同时使用@Bean
package com.hengtiansoft.framework.common.framework.config;
import com.hengtiansoft.framework.common.util.Threads;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池配置
*
* @author shengwangzhong
*/
@Configuration
public class ThreadPoolConfig {
private static final int CORE_POOL_SIZE = 60;
private static final int MAX_POOL_SIZE = 200;
private static final int QUEUE_CAPACITY = 1000;
private static final int KEEP_ALIVE_SECONDS = 300;
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
@Bean(name = "threadPoolExecutor")
public ThreadPoolExecutor threadPoolExecutor() {
return new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(QUEUE_CAPACITY), new ThreadPoolExecutor.CallerRunsPolicy());
}
@Bean(name = "extractDataExecutor")
public ThreadPoolExecutor extractDataExecutor() {
return new ThreadPoolExecutor(30, 60, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(500), new ThreadPoolExecutor.CallerRunsPolicy());
}
@Bean(name = "batchTaskExecutor")
public ThreadPoolTaskExecutor batchTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setMaxPoolSize(100);
executor.setCorePoolSize(80);
executor.setQueueCapacity(800);
executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
/**
* 执行周期性或定时任务
*/
@Bean(name = "scheduledExecutorService")
protected ScheduledExecutorService scheduledExecutorService() {
return new ScheduledThreadPoolExecutor(CORE_POOL_SIZE,
new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
Threads.printException(r, t);
}
};
}
@Bean(name = "exportThreadPoolExecutor")
public ThreadPoolExecutor exportThreadPoolExecutor() {
return new ThreadPoolExecutor(2, 2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(QUEUE_CAPACITY), new ThreadPoolExecutor.CallerRunsPolicy());
}
}
在需要调用的位置,使用@Autowired 获取即可
@Autowired
private ThreadPoolExecutor threadPoolExecutor;
调用当前线程
threadPoolExecutor.execute(new Thread(new OldMsg(sopTodoListOld,1)));