Spring中實(shí)現(xiàn)多線程,其實(shí)非常簡(jiǎn)單,只需要在配置類中添加@EnableAsync就可以使用多線程。在希望執(zhí)行的并發(fā)方法中使用@Async就可以定義一個(gè)線程任務(wù)。通過(guò)spring給我們提供的ThreadPoolTaskExecutor就可以使用線程池。
第一步,先在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
24
25
26
27
28
29
30
31
|
package com.jmxf.core.config; import java.util.concurrent.Executor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration @EnableAsync // 啟用異步任務(wù) public class AsyncConfiguration { // 組件計(jì)算 @Bean ( "zjExecutor" ) public Executor asyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //核心線程數(shù)5:線程池創(chuàng)建時(shí)候初始化的線程數(shù) executor.setCorePoolSize( 5 ); //最大線程數(shù)5:線程池最大的線程數(shù),只有在緩沖隊(duì)列滿了之后才會(huì)申請(qǐng)超過(guò)核心線程數(shù)的線程 executor.setMaxPoolSize( 10 ); //緩沖隊(duì)列500:用來(lái)緩沖執(zhí)行任務(wù)的隊(duì)列 executor.setQueueCapacity( 500 ); //允許線程的空閑時(shí)間60秒:當(dāng)超過(guò)了核心線程出之外的線程在空閑時(shí)間到達(dá)之后會(huì)被銷毀 executor.setKeepAliveSeconds( 60 ); //線程池名的前綴:設(shè)置好了之后可以方便我們定位處理任務(wù)所在的線程池 executor.setThreadNamePrefix( "DailyAsync-" ); executor.initialize(); return executor; } } |
有很多你可以配置的東西。默認(rèn)情況下,使用SimpleAsyncTaskExecutor。
第二步,使用線程池
在定義了線程池之后,我們?nèi)绾巫尞惒秸{(diào)用的執(zhí)行任務(wù)使用這個(gè)線程池中的資源來(lái)運(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
32
33
|
package com.jmxf.service.fkqManage.zj; import org.springframework.scheduling.annotation.Async; @Service public class CentreZj { /** * 多線程執(zhí)行 zj計(jì)算推數(shù) * @param fkqZj * @throws Exception */ @Async ( "zjExecutor" ) public CompletableFuture<String> executeZj (FkqZj fkqZj) { if (fkqZj == null ) return ; String zjid = fkqZj.getZjid(); FkqHdzjdm zjdm = getZjdm(zjid); String zjlj = zjdm.getZjlj(); if (StringUtils.isBlank(zjlj)) return ; Object bean = ApplicationContextProvider.getBean(zjlj); Method method; try { method = bean.getClass().getMethod( "refresh" ,String. class ); method.invoke(bean,zjid); } catch (Exception e) { e.printStackTrace(); } } return CompletableFuture.completedFuture(zjid); } |
executeZj方法被標(biāo)記為Spring的 @Async 注解,表示它將在一個(gè)單獨(dú)的線程上運(yùn)行。該方法的返回類型是 CompleetableFuture 而不是 String,這是任何異步服務(wù)的要求。
第三步,調(diào)用測(cè)試
1
2
3
4
5
6
7
8
9
|
List<CompletableFuture<String>> executeZjs = new ArrayList<>(); for (FkqZj fkqZj : zjs) { CompletableFuture<String> executeZj = centreZj.executeZj(fkqZj); executeZjs.add(executeZj); } //等待所以子線程結(jié)束后 返回結(jié)果 for (CompletableFuture<String> completableFuture : executeZjs) { CompletableFuture.allOf(completableFuture).join(); } |
注意事項(xiàng)
異步方法和調(diào)用方法一定要寫在不同的類中 ,如果寫在一個(gè)類中,是沒(méi)有效果的!
原因:
spring對(duì)@Transactional注解時(shí)也有類似問(wèn)題,spring掃描時(shí)具有@Transactional注解方法的類時(shí),是生成一個(gè)代理類,由代理類去開(kāi)啟關(guān)閉事務(wù),而在同一個(gè)類中,方法調(diào)用是在類體內(nèi)執(zhí)行的,spring無(wú)法截獲這個(gè)方法調(diào)用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/MisMe/p/13390092.html