springboot 初始化线程池_springboot项目中线程池的使用
在application.properties添加線(xiàn)程池配置項(xiàng)
spring.task.scheduling.thread-name-prefix=SysAsyncExecutor_
spring.task.scheduling.pool.size=10
spring.task.execution.thread-name-prefix=SysAsyncExecutor_
spring.task.execution.pool.core-size=6
spring.task.execution.pool.max-size=200
spring.task.execution.pool.queue-capacity=10
spring.task.execution.pool.keep-alive=60
spring.task.execution.pool.allow-core-thread-timeout=true
公共線(xiàn)程池配置類(lèi)
@Slf4j
@Configuration
@EnableAsync
public class ThreadAsyncConfigurer implements AsyncConfigurer {
@Value("${spring.task.execution.thread-name-prefix:thread_name_prefix_}")
private String threadNamePrefix;
@Value("${spring.task.scheduling.pool.size:10}")
private int corePoolSize;
@Value("${spring.task.execution.pool.max-size:100}")
private int maxPoolSize;
@Value("${spring.task.execution.pool.queue-capacity:10}")
private int queueCapacity;
@Value("${spring.task.execution.pool.keep-alive:60}")
private int keepAliveSeconds;
private int awaitTerminationMillis = 60;
private boolean waitForJobsToCompleteOnShutdown = Boolean.TRUE;
@Bean("taskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
//設(shè)置核心線(xiàn)程數(shù)
threadPool.setCorePoolSize(corePoolSize);
//設(shè)置最大線(xiàn)程數(shù)
threadPool.setMaxPoolSize(maxPoolSize);
//線(xiàn)程池所使用的緩沖隊(duì)列
threadPool.setQueueCapacity(queueCapacity);
//等待任務(wù)在關(guān)機(jī)時(shí)完成--表明等待所有線(xiàn)程執(zhí)行完
threadPool.setWaitForTasksToCompleteOnShutdown(waitForJobsToCompleteOnShutdown);
//等待時(shí)間 (默認(rèn)為0,此時(shí)立即停止),并沒(méi)等待xx秒后強(qiáng)制停止
threadPool.setAwaitTerminationSeconds(awaitTerminationMillis);
//線(xiàn)程空閑后的最大存活時(shí)間
threadPool.setKeepAliveSeconds(keepAliveSeconds);
//線(xiàn)程名稱(chēng)前綴
threadPool.setThreadNamePrefix(threadNamePrefix);
//rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)
//CALLER_RUNS:不在新線(xiàn)程中執(zhí)行任務(wù),而是有調(diào)用者所在的線(xiàn)程來(lái)執(zhí)行
threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPool.setThreadNamePrefix(threadNamePrefix);// 線(xiàn)程名稱(chēng)前綴
//初始化線(xiàn)程
threadPool.initialize();
return threadPool;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
//return new SimpleAsyncUncaughtExceptionHandler();
return new AsyncExceptionHandler();
}
/**
* 自定義異常處理類(lèi)
*
* @author liunh
*/
class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
log.info("SysAsyncExecutor Exception message - {}", throwable.getMessage());
log.info("SysAsyncExecutor Exception Method name - {}", method.getName());
for (Object param : obj) {
log.info("SysAsyncExecutor Exception Parameter value - " + param);
}
}
}
}
使用
@Slf4j
@SpringBootTest(classes = SpringbootConcurrentDemoApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class AsyncTaskServiceTest {
@Resource(name = "taskExecutor")
private ThreadPoolTaskExecutor executor;
@Test
public void asyncExecutorTask() {
Listfutures = new ArrayList<>();
ListuserList = new ArrayList<>(Arrays.asList(
"123@qq.com",
"456@qq.com"
));
for (String email : userList) {
Future> future = executor.submit(() -> {
new SendEmailThread(email);
});
futures.add(future);
}
log.info(">>>>>>>>>>>>>>>>end");
}
class SendEmailThread implements Callable{
private String to;
public SendEmailThread(String to) {
this.to = to;
}
@Override
public String call() throws Exception {
log.info(">>>>>>>>>>>>>>>>發(fā)送郵件,接收郵箱:{}", to);
return "接收郵箱:" + to;
}
}
}
文章來(lái)源: www.oschina.net,作者:Lion華,版權(quán)歸原作者所有,如需轉(zhuǎn)載,請(qǐng)聯(lián)系作者。
原文鏈接:https://my.oschina.net/lion1220/blog/3221674
總結(jié)
以上是生活随笔為你收集整理的springboot 初始化线程池_springboot项目中线程池的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最低工资标准的最新统计已经完成,哪些人和
- 下一篇: 三峰申购做什么的