wangmengmeng
2024-12-24 24432a361d5c6bd6f3d8c008693e9f1155d62517
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
package com.dji.sample.configuration;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.concurrent.*;
 
/**
 *
 * @author sean.zhou
 * @date 2021/11/10
 * @version 0.1
 */
@Configuration
public class GlobalThreadPoolConfiguration {
 
    @Value("${thread.pool.core-pool-size: 10}")
    private int corePoolSize;
 
    @Value("${thread.pool.maximum-pool-size: 20}")
    private int maximumPoolSize;
 
    @Value("${thread.pool.keep-alive-time: 60}")
    private long keepAliveTime;
 
    @Value("${thread.pool.queue.capacity: 1000}")
    private int capacity;
 
    /**
     * A custom thread pool.
     * @return
     */
    @Bean
    public Executor threadPool() {
        return new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize, keepAliveTime,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(capacity),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
    }
 
}