定义线程池
/**
* 自定义线程池,推荐使用
*
* @return XyThreadPoolTaskExecutor
*/
@Bean("threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
//一定要这一行
XyThreadPoolConfig config = XyThreadPoolConfig.init(this.threadPoolConfig);
RejectedExecutionHandler rejectedExecutionHandler = new CallerRunsPolicy();
if (threadPoolConfig.getHandlerEnum() != null) {
String handlerName = threadPoolConfig.getHandlerEnum().name();
if ("DISCARD_POLICY".equals(handlerName)) {
rejectedExecutionHandler = new DiscardPolicy();
} else if ("ABORT_POLICY".equals(handlerName)) {
rejectedExecutionHandler = new AbortPolicy();
} else if ("DISCARD_OLDEST_POLICY".equals(handlerName)) {
rejectedExecutionHandler = new DiscardOldestPolicy();
}
}
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(config.getCoreSize());
executor.setMaxPoolSize(config.getMaxSize());
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
executor.setQueueCapacity(config.getQueueCapacity());
executor.setAllowCoreThreadTimeOut(true);
//executor.setTaskDecorator();
//executor.setThreadFactory();
executor.setThreadNamePrefix(config.getThreadNamePrefix());
executor.setRejectedExecutionHandler(rejectedExecutionHandler);
//表明等待所有线程执行完
executor.setWaitForTasksToCompleteOnShutdown(true);
//等待xx秒后强制停止
executor.setAwaitTerminationSeconds(10);
//executor.setAwaitTerminationMillis();
executor.setBeanName("xyThreadPoolTaskExecutor");
executor.setThreadPriority(config.getThreadPriority());
executor.setDaemon(false);
executor.setThreadGroupName(config.getThreadGroupName());
executor.afterPropertiesSet();
return executor;
}
配置线程池
在CommonConfig定义了默认的线程池,如需修改线程池的配置,在配置文件中添加相应配置即可。
xy.core.thread.pool.config.core-size=2
xy.core.thread.pool.config.max-size=20
xy.core.thread.pool.config.queue-capacity=200
xy.core.thread.pool.config.thread-name-prefix=sample
xy.core.thread.pool.config.thread-priority=5
xy.core.thread.pool.config.allow-core-thread-time-out=true
xy.core.thread.pool.config.keep-alive-seconds=3000
xy.core.thread.pool.config.thread-group-name=group1
xy.core.thread.pool.config.thread-pool-name=async-pool
xy.core.thread.pool.config.handler-enum=DISCARD_POLICY
若没有显示声明配置,则会提供默认的配置
tpc.setCoreSize(2 * Runtime.getRuntime().availableProcessors() + 1);
tpc.setMaxSize(2 * Runtime.getRuntime().availableProcessors() + 1);
tpc.setThreadNamePrefix(xyThreadPoolConfig.getThreadPoolName() + "-xyThread-");
tpc.setKeepAliveSeconds(100);
tpc.setQueueCapacity(200);
tpc.setThreadGroupName(XyEnvironmentUtils.getApplicationName());
tpc.setThreadPriority(5);
使用线程池
线程池通常做异步方法执行使用,生产的应用框架中也自带了样例。
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async("sampleThreadPool")
public void test(String name) {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
String message = MessageFormat.format("threadId-:{0},threadName-:{1},call:{2}", Thread.currentThread().getId(), Thread.currentThread().getName(), name);
log.error(message);
}
}