前言
在之前的Spring Boot基礎(chǔ)教程系列中,已經(jīng)通過《Spring Boot中使用@Async實(shí)現(xiàn)異步調(diào)用》一文介紹過如何使用@Async注解來實(shí)現(xiàn)異步調(diào)用了。但是,對(duì)于這些異步執(zhí)行的控制是我們保障自身應(yīng)用健康的基本技能。本文我們就來學(xué)習(xí)一下,如果通過自定義線程池的方式來控制異步調(diào)用的并發(fā)。
本文中的例子我們可以在之前的例子基礎(chǔ)上修改,也可以創(chuàng)建一個(gè)全新的Spring Boot項(xiàng)目來嘗試。
定義線程池
第一步,先在Spring Boot主類中定義一個(gè)線程池,比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application. class , args); } @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()); return executor; } } } |
上面我們通過使用ThreadPoolTaskExecutor創(chuàng)建了一個(gè)線程池,同時(shí)設(shè)置了以下這些參數(shù):
- 核心線程數(shù)10:線程池創(chuàng)建時(shí)候初始化的線程數(shù)
- 最大線程數(shù)20:線程池最大的線程數(shù),只有在緩沖隊(duì)列滿了之后才會(huì)申請(qǐng)超過核心線程數(shù)的線程
- 緩沖隊(duì)列200:用來緩沖執(zhí)行任務(wù)的隊(duì)列
- 允許線程的空閑時(shí)間60秒:當(dāng)超過了核心線程出之外的線程在空閑時(shí)間到達(dá)之后會(huì)被銷毀
- 線程池名的前綴:設(shè)置好了之后可以方便我們定位處理任務(wù)所在的線程池
- 線程池對(duì)拒絕任務(wù)的處理策略:這里采用了CallerRunsPolicy策略,當(dāng)線程池沒有處理能力的時(shí)候,該策略會(huì)直接在 execute 方法的調(diào)用線程中運(yùn)行被拒絕的任務(wù);如果執(zhí)行程序已關(guān)閉,則會(huì)丟棄該任務(wù)
使用線程池
在定義了線程池之后,我們?nèi)绾巫尞惒秸{(diào)用的執(zhí)行任務(wù)使用這個(gè)線程池中的資源來運(yùn)行呢?方法非常簡(jiǎn)單,我們只需要在@Async注解中指定線程池名即可,比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@Slf4j @Component public class Task { public static Random random = new Random(); @Async ( "taskExecutor" ) public void doTaskOne() throws Exception { log.info( "開始做任務(wù)一" ); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt( 10000 )); long end = System.currentTimeMillis(); log.info( "完成任務(wù)一,耗時(shí):" + (end - start) + "毫秒" ); } @Async ( "taskExecutor" ) public void doTaskTwo() throws Exception { log.info( "開始做任務(wù)二" ); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt( 10000 )); long end = System.currentTimeMillis(); log.info( "完成任務(wù)二,耗時(shí):" + (end - start) + "毫秒" ); } @Async ( "taskExecutor" ) public void doTaskThree() throws Exception { log.info( "開始做任務(wù)三" ); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt( 10000 )); long end = System.currentTimeMillis(); log.info( "完成任務(wù)三,耗時(shí):" + (end - start) + "毫秒" ); } } |
單元測(cè)試
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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(); } } |
執(zhí)行上面的單元測(cè)試,我們可以在控制臺(tái)中看到所有輸出的線程名前都是之前我們定義的線程池前綴名開始的,說明我們使用線程池來執(zhí)行異步任務(wù)的試驗(yàn)成功了!
2018-03-27 22:01:15.620 INFO 73703 --- [ taskExecutor-1] com.didispace.async.Task : 開始做任務(wù)一
2018-03-27 22:01:15.620 INFO 73703 --- [ taskExecutor-2] com.didispace.async.Task : 開始做任務(wù)二
2018-03-27 22:01:15.620 INFO 73703 --- [ taskExecutor-3] com.didispace.async.Task : 開始做任務(wù)三
2018-03-27 22:01:18.165 INFO 73703 --- [ taskExecutor-2] com.didispace.async.Task : 完成任務(wù)二,耗時(shí):2545毫秒
2018-03-27 22:01:22.149 INFO 73703 --- [ taskExecutor-3] com.didispace.async.Task : 完成任務(wù)三,耗時(shí):6529毫秒
2018-03-27 22:01:23.912 INFO 73703 --- [ taskExecutor-1] com.didispace.async.Task : 完成任務(wù)一,耗時(shí):8292毫秒
完整示例:
讀者可以根據(jù)喜好選擇下面查看Chapter4-1-3項(xiàng)目:
Github:https://github.com/dyc87112/SpringBoot-Learning/
Gitee:https://gitee.com/didispace/SpringBoot-Learning/
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:http://blog.didispace.com/springbootasync-2/