xiangpei
2025-05-27 f2cc79e9e83aafacc1af0e2c86e3c8df384fc895
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.lili.common.thread;
 
import cn.lili.common.properties.ThreadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.Executor;
 
/**
 * 多线程配置
 *
 * @author Chopper
 * @version v1.0
 * @since
 * 2020-03-12 10:50 上午
 */
@Configuration
public class ThreadConfig implements AsyncConfigurer {
 
 
    @Autowired
    private ThreadProperties threadProperties;
 
 
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//       核心线程数,默认为5
        executor.setCorePoolSize(threadProperties.getCorePoolSize());
//       最大线程数,默认为10
        executor.setMaxPoolSize(threadProperties.getMaxPoolSize());
//     队列最大长度,一般需要设置值为足够大
        executor.setQueueCapacity(threadProperties.getQueueCapacity());
//       线程池维护线程所允许的空闲时间,默认为60s
        executor.setKeepAliveSeconds(threadProperties.getKeepAliveSeconds());
//       允许超时关闭
        executor.setAllowCoreThreadTimeOut(threadProperties.getAllowCoreThreadTimeOut());
        executor.initialize();
        return executor;
    }
}