springboot中线程池配置及使用
1、MyThreadPoolExecutorService.java 自定义线程池ExecutorService 的bean
import lombok.extern.slf4j.Slf4j; import java.util.Arrays; import java.util.concurrent.*; @Slf4j public class MyThreadPoolExecutorService extends ThreadPoolExecutor{ public MyThreadPoolExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } public MyThreadPoolExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); } public MyThreadPoolExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); } public MyThreadPoolExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } @Override protected void beforeExecute(Thread t, Runnable r) { super.beforeExecute(t, r); log.info("beforeExecute: activeCount={}, taskCount={}",this.getActiveCount(),this.getTaskCount()); } @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); log.info("afterExecute: activeCount={}, taskCount={}",this.getActiveCount(),this.getTaskCount()); if (t!=null){ log.error("执行错误:{} ",t.getMessage()); log.error("详细信息:{}", Arrays.asList(t.getStackTrace())); } } @Override protected void terminated() { super.terminated(); log.info("terminated: activeCount={}, taskCount={}",this.getActiveCount(),this.getTaskCount()); } }
2、ThreadPoolConfig.java 配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.*; /** * 自定义线程池 ExecutorService配置 */ @Configuration public class ThreadPoolConfig { /** * 线程池的注解 * @return */ @Bean public EccloudThreadPoolExecutorService executorService(){ EccloudThreadPoolExecutorService executorService = new EccloudThreadPoolExecutorService(50,100, 60, TimeUnit.SECONDS,new ArrayBlockingQueue<>(500), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); return executorService; } }
3、AsyncConfig 开启@Async 异步支持
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; /** * 开启Async异步调用支持, * 开启spring-task任务调度 */ @Configuration @EnableAsync @EnableScheduling public class AsyncConfig { /** * 自定义异步线程的执行器 ,springboot中默认寻找的执行器名称为"taskExecutor" */ @Bean public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 设置核心线程数 executor.setCorePoolSize(20); // 设置最大线程数 executor.setMaxPoolSize(50); // 设置队列容量 executor.setQueueCapacity(20); // 设置线程活跃时间(秒) executor.setKeepAliveSeconds(60); // 设置线程名称前缀 executor.setThreadNamePrefix("spring-async-task"); // 设置拒绝策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任务结束后再关闭线程 executor.setWaitForTasksToCompleteOnShutdown(true); executor.initialize(); return executor; } }
4、如何使用异步调用
@Autowired
private ExecutorService executorService; //注入executorService bean
在业务方法中使用以下代码执行异步逻辑
executorService.execute(()->{
//业务逻辑
});
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1