1. 程式人生 > >Springboot的異步線程池圖鑒

Springboot的異步線程池圖鑒

ask 設置 tar line exe dot conf setw close

1:定義線程池

技術分享圖片
@EnableAsync
    @Configuration
    class TaskPoolConfig {
 
        @Bean("taskExecutor")
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(10);
            executor.setMaxPoolSize(
20); executor.setQueueCapacity(200); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("taskExecutor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(
60); return executor; } }
View Code

2:如何使用該線程池呢?

技術分享圖片
@Slf4j
@Component
public class Task {
 
    public static Random random = new Random();
@Autowired
    private StringRedisTemplate stringRedisTemplate;
@Async("taskExecutor")
    public void doTaskOne() throws Exception {
        log.info(
"開始做任務一"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info(stringRedisTemplate.randomKey()); log.info("完成任務一,耗時:" + (end - start) + "毫秒"); } @Async("taskExecutor") public void doTaskTwo() throws Exception { log.info("開始做任務二"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info("完成任務二,耗時:" + (end - start) + "毫秒"); } @Async("taskExecutor") public void doTaskThree() throws Exception { log.info("開始做任務三"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info("完成任務三,耗時:" + (end - start) + "毫秒"); } }
View Code

3 執行異步任務

技術分享圖片
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
 
    @Autowired
    private Task task;
 
    @Test
    public void test() throws Exception {
 
        task.doTaskOne();
        task.doTaskTwo();
        task.doTaskThree();
 
        Thread.currentThread().join();
    }
 
}
View Code

4 打印結果

技術分享圖片
2018-03-27 22:01:15.620  INFO 73703 --- [ taskExecutor-1] com.didispace.async.Task  : 開始做任務一

2018-03-27 22:01:15.620  INFO 73703 --- [ taskExecutor-2] com.didispace.async.Task  : 開始做任務二
2018-03-27 22:01:15.620  INFO 73703 --- [ taskExecutor-3] com.didispace.async.Task  : 開始做任務三
2018-03-27 22:01:18.165  INFO 73703 --- [ taskExecutor-2] com.didispace.async.Task  : 完成任務二,耗時:2545毫秒
2018-03-27 22:01:22.149  INFO 73703 --- [ taskExecutor-3] com.didispace.async.Task  : 完成任務三,耗時:6529毫秒
2018-03-27 22:01:23.912  INFO 73703 --- [ taskExecutor-1] com.didispace.async.Task  : 完成任務一,耗時:8292毫秒
View Code

5 註意事項

註: @Async所修飾的函數不要定義為static類型,這樣異步調用不會生效

從異常信息JedisConnectionException: Could not get a resource from the pool來看,我們很容易的可以想到,在應用關閉的時候異步任務還在執行,由於Redis連接池先銷毀了,導致異步任務中要訪問Redis的操作就報了上面的錯。所以,我們得出結論,上面的實現方式在應用關閉的時候是不優雅的,那麽我們要怎麽做呢?如下設置:

executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60);

Springboot的異步線程池圖鑒