|
@ -0,0 +1,35 @@
|
|
|
|
package com.yihu.jw.care.config;
|
|
|
|
|
|
|
|
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created with IntelliJ IDEA.
|
|
|
|
*
|
|
|
|
* @Author: yeshijie
|
|
|
|
* @Date: 2021/4/30
|
|
|
|
* @Description: 异步线程配置
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
public class AsyncConfig {
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public Executor taskExecutor() {
|
|
|
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
|
executor.setCorePoolSize(5); // 设置核心线程数
|
|
|
|
executor.setMaxPoolSize(20); // 设置最大线程数
|
|
|
|
executor.setQueueCapacity(200); // 设置队列容量
|
|
|
|
executor.setKeepAliveSeconds(60); // 设置线程活跃时间(秒)
|
|
|
|
executor.setThreadNamePrefix("user-device-"); // 设置默认线程名称
|
|
|
|
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
|
|
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
|
|
|
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 设置拒绝策略
|
|
|
|
executor.setWaitForTasksToCompleteOnShutdown(true); // 等待所有任务结束后再关闭线程池
|
|
|
|
executor.initialize();
|
|
|
|
return executor;
|
|
|
|
}
|
|
|
|
}
|