.pool.coreSize:50}")
private int coreSize;
@Value("${thread.pool.maxSize:50}")
private int maxSize;
@Value("${thread.pool.queueSize:9999}")
private int queueSize;
@Value("${thread.pool.threadNamePrefix:thread-name}")
private String threadNamePrefix;
@Value("${thread.pool.keepAlive:60}")
private int keepAlive;
@Autowired
private ThreadExceptionLogHandler threadExceptionLogHandler;
/**
* 方式一: new VisiableThreadPoolTaskExecutor() 方式创建线程池,返回值是 Executor
* 适用于 @Async("asyncServiceExecutor") 注解
* 也可以
* @Autowired
* private Executor asyncServiceExecutor;
*
* @return Executor
*/
@Bean
public Executor asyncServiceExecutor() {
VisiableThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
// 配置核心线程数 50
executor.setCorePoolSize(coreSize);
// 配置最大线程数 50
executor.setMaxPoolSize(maxSize);
// 配置队列大小 9999
executor.setQueueCapacity(queueSize);
// 配置线程池中的线程名称前缀 模块-功能-作用
executor.setThreadNamePrefix(threadNamePrefix);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
// 线程池无法接受新的任务并且队列已满时,如果有新的任务提交给线程池,而线程池已经达到了最大容量限制,那么这个任务不会被丢弃,而是由调用该任务的线程来执行。
// 这样可以避免任务被直接丢弃,并让调用者自己执行任务以减轻任务提交频率。
// 这个拒绝策略可能会导致任务提交者的线程执行任务,这可能会对调用者的性能产生一些影响,因为调用者线程需要等待任务执行完成才能继续进行其他操作。
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 线程空闲后的最大存活时间 60
executor.setKeepAliveSeconds(keepAlive);
// 执行初始化
executor.initialize();
return executor;
}
/**
* 方式二: new ThreadPoolExecutor() 方式创建线程池
* 适用于:
* @Autowired
* private ExecutorService fbWorkerPool;
* @return ExecutorService
*/
@Bean
public ExecutorService workerPool() {
return new ThreadPoolExecutor(coreSize, maxSize, keepAlive, TimeUnit.MILLISECONDS,
new LinkedBlockingDeque<>(20000),
new ThreadFactory() {
private final AtomicInteger threadNumber = new AtomicInteger(1);
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, threadNamePrefix + threadNumber.getAndIncrement());
thread.setUncaughtExceptionHandler(threadExceptionLogHandler);
return thread;
}
});
}
}
ExecutorController 编写
- 演示demo,三种不同的用法, 足以涵盖大部分场景
点击查看代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
/**
* 这里是demo演示、把业务写在 controller 了,一般开发都是在 service 层实现的。
*
* @author love ice
* @create 2023-09-19 0:59
*/
@RestController
@RequestMapping("/executor")
public class ExecutorController {
/**
* demo1: 使用异步注解 @Async("asyncServiceExecutor") 执行方法,适用于没有返回值的情况下
*/
public void asyncDemo1() {
// 假设这是从数据库查询出来的数据
List<String> nameList = new ArrayList<>(Arrays.asList("张三", "李四", "王五"));
// 把 nameList 进行切分
int j = 0, size = nameList.size(), batchSize = 10;
List<List<String>> list = new Arra
|