Springboot线程池配置
最近的项⽬⾥要⼿动维护线程池,然后看到⼀起开发的⼩伙伴直接⽤Java了,我坚信Springboot不可能没这功能,于是查了些资料,果然有,这⾥给⼀下。
⾸先我们都知道@Async标签能让⽅法异步执⾏,但是这个标签⽤的是Springboot默认的线程池,想⾃⼰实现线程池就要在项⽬⾥创建⼀个TaskExecutor或它的⼦类的Bean,像这样:
@Bean
public AsyncTaskExecutor threadPoolTaskExecutor(){
ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
//加⼊此头后此线程池成为系统线程池
threadPoolTaskExecutor.setThreadNamePrefix("Anno-Executor");
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return threadPoolTaskExecutor;
}
其中拒绝策略可以改为⼿动编写,像下⾯这样:
threadPoolTaskExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
java线程池创建的四种@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
}
});
JDK⾥提供了四种默认的策略,⾮常粗暴:
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
/**
* Executes task r in the caller's thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
/**
* A handler for rejected tasks that throws a
* {@code RejectedExecutionException}.
*/
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
}
}
/**
* A handler for rejected tasks that silently discards the
* rejected task.
*/
public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
/**
* A handler for rejected tasks that discards the oldest unhandled
* request and then retries {@code execute}, unless the executor
* is shut down, in which case the task is discarded.
*/
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
}
}
}
CallerRunsPolicy是让调⽤⽅法的线程⾃⼰运⾏。
AbortPolicy丢弃任务抛出异常。
DiscardPolicy:丢弃任务不报错。
DiscardOldestPolicy:丢弃队列最前⾯任务,之后尝试执⾏。
顺便说⼀下,查源码可知,默认的策略是AbortPolicy,也就是最粗暴的那个,不过考虑到通常是不要让拒绝发⽣的,这⾥⽤粗暴的⽅案问题不⼤。为了实现⽅便配置,可以⽤yml对其进⾏配置:
@Component
@Data
@ConfigurationProperties("thread-pool-factory")
public class ThreadPoolFactory {
private int corePoolSize;
private int maxPoolSize;
private int queueCapacity;
private int keepAliveSeconds;
@Bean
public AsyncTaskExecutor threadPoolTaskExecutor(){
//......
}
}
这⾥@Data标签是lambok的标签,快速⽣成getter和setter⽤。以上Component构建好后,可以直接配置:
thread-pool-factory:
#IO密集型应⽤,线程数为2N+1
corePoolSize: 9
maxPoolSize: 18
queueCapacity: 100
keepAliveSeconds: 120
以上。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。