前言
在前面,我们简单学习了Springboot中异步处理@Async小demo。前面的异步中,我们在需要异步的方法上加上@Async注解。@Async默认异步配置使用的是SimpleAsyncTaskExecutor,该线程池默认来一个任务创建一个线程,这种方式可能会在高并发场景下出现OOM。OutOfMemoryError:unable to create new native thread,创建线程数量太多,占用内存过大。一般在实际项目中我们不会使用SimpleAsyncTaskExecutor这种线程池,而是根据实际场景来自定义线程池。下面就和大家一起学习下如何自定义线程池。
线程池配置
对于线程池的优点,想必大家都知道,这里就不赘述了。我们直接上demo。首先我们需要自定义一个线程池配置类
/**
* @author 蒋墨风
* @title: ThreadPoolConfig
* @projectName study
* @description: 线程池配置类
* @date 2019/6/3010:54
*/
@Configuration
public class ThreadPoolConfig {
@Bean("getThreadPoolExecutor")
public ThreadPoolTaskExecutor getThreadPoolExecutor(){
ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(30);
threadPoolTaskExecutor.setQueueCapacity(100);
threadPoolTaskExecutor.setKeepAliveSeconds(60);
threadPoolTaskExecutor.setThreadNamePrefix("aiqinhai-thread");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.setAwaitTerminationSeconds(60);
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
上面我们通过使用ThreadPoolTaskExecutor
创建了一个线程池,同时设置了以下这些参数:
- 核心线程数10:线程池创建时候初始化的线程数
- 最大线程数30:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
- 缓冲队列100:用来缓冲执行任务的队列
- 允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
- 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
- 线程池对拒绝任务的处理策略:这里采用了
CallerRunsPolicy
策略,当线程池没有处理能力的时候,该策略会直接在 execute 方法的调用线程中运行被拒绝的任务;如果执行程序已关闭,则会丢弃该任务 - 关闭线程池的时候,是否等待当前任务执行完成。
- 等待当前任务完成的超时时间60秒,如果一直等待就会造成阻塞。
线程池使用
上面线程池已经定义好了,那么我们怎么在异步中使用上面定义的线程池呢,其实很简单,只需要在@Async指定线程池。
**
* @author 蒋墨风
* @title: AsyncTestWithThreadPool
* @projectName study
* @description: 线程池使用demo
* @date 2019/6/30 16:38
*/
@Async("getThreadPoolExecutor")
@Component
public class AsyncTestWithThreadPool {
private static final Logger LOGGER=LoggerFactory.getLogger(AsyncTestWithThreadPool.class);
public void testWithThreadPool1() {
try {
Thread.sleep(2000);
LOGGER.info("testWithThreadPool1当前线程是{}",Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testWithThreadPool2() {
try {
Thread.sleep(4000);
LOGGER.info("testWithThreadPool2当前线程是{}",Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testWithThreadPool3() {
try {
Thread.sleep(6000);
LOGGER.info("testWithThreadPool3当前线程是{}",Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试
@RequestMapping("/testAsyncWithThreadPool")
public void testAsyncWithThreadPool() {
LOGGER.info("开始运行testAsyncWithThreadPool方法!");
syncTestWithThreadPool.testWithThreadPool1();
syncTestWithThreadPool.testWithThreadPool2();
syncTestWithThreadPool.testWithThreadPool3();
LOGGER.info("testAsyncWithThreadPool方法结束!");
}
测试结果
可以看到10个线程建立好了之后,没有继续创建11和12了,而是直接复用了之前的线程。
总结
上面简单介绍了springboot中的线程池使用,理解配置中的每一个参数,在项目中更好的优化还是很有必要的。后面在和大家一起学习下springboot的异步结果获取。