|
@ -3,51 +3,39 @@ 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.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2016.10.18.
|
|
|
* 启用多綫程
|
|
|
* Created with IntelliJ IDEA.
|
|
|
*
|
|
|
* @Author: yeshijie
|
|
|
* @Date: 2021/4/30
|
|
|
* @Description: 异步线程配置
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableAsync
|
|
|
public class AsyncConfig {
|
|
|
/**
|
|
|
* 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程
|
|
|
*/
|
|
|
private int corePoolSize = 5;
|
|
|
/**
|
|
|
* 如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入queueCapacity.
|
|
|
* 设定 比 系统native thread个数要大的话,会优先抛出Java.lang.OutOfMemoryError: unable to create new native thread
|
|
|
*/
|
|
|
private int maxPoolSize = 10;
|
|
|
/**
|
|
|
* 缓冲队列大小
|
|
|
*/
|
|
|
private int queueCapacity = 100;
|
|
|
|
|
|
/**
|
|
|
* 线程池维护线程所允许的空闲时间 秒
|
|
|
*/
|
|
|
private int keepAliveSeconds = 300;
|
|
|
|
|
|
@Bean
|
|
|
public Executor taskExecutor() {
|
|
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
executor.setCorePoolSize(corePoolSize);
|
|
|
executor.setMaxPoolSize(maxPoolSize);
|
|
|
executor.setQueueCapacity(queueCapacity);
|
|
|
executor.setKeepAliveSeconds(keepAliveSeconds);
|
|
|
/** Reject策略预定义有四种:
|
|
|
(1)ThreadPoolExecutor.AbortPolicy策略,是默认的策略,处理程序遭到拒绝将抛出运行时 RejectedExecutionException。
|
|
|
(2)ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
|
|
|
(3)ThreadPoolExecutor.DiscardPolicy策略,不能执行的任务将被丢弃.
|
|
|
(4)ThreadPoolExecutor.DiscardOldestPolicy策略,如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程).
|
|
|
*/
|
|
|
// 设置核心线程数
|
|
|
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;
|
|
|
}
|