SpringBoot下的异步线程池

Posted by youthred on April 15, 2021
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
44
45
46
47
48
49
50
package net.add1s.config.thread;

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.ThreadPoolExecutor;

/**
 * 须在启动类上添加注解“@EnableAsync”
 * 执行线程的方法必须被Spring容器管理,且不能使用“static”修饰,如在@Service修饰的实现类的方法上添加注解“@Async("taskExecutor")”后就可以正常使用异步线程
 * 执行异步线程的方法不能与调用方法同类
 *
 * @author pj.w@qq.com
 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {

    /**
     * 核心(默认)线程数
     */
    private static final int CORE_POOL_SIZE = 20;

    /**
     * 最大线程数
     */
    private static final int MAX_POOL_SIZE = 100;

    /**
     * 允许线程空闲的时间(秒)
     */
    private static final int KEEP_ALIVE_TIME = 10;

    /**
     * 缓冲队列大小
     */
    private static final int QUEUE_CAPACITY = 200;

    /**
     * 线程池名称前缀
     */
    private static final String THREAD_NAME_PREFIX = "Async-Service-";

    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        return new ThreadPoolTaskExecutor() ;
    }
}

how to use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 伪代码
@Component
class XXX {

    @Async("taskExecutor")
    public void runT_1() { // TODO }

    @Async("taskExecutor")
    public void runT_2() { // TODO }
}

// 伪代码
class XXX2 {

    @Autowired
    private XXX xxx;

    psvm() {
        xxx.runT_1();
        xxx.runT_2();
    }
}