Java并發(fā)之異步的八種實(shí)現(xiàn)方式
在實(shí)際開發(fā)中,很多業(yè)務(wù)場(chǎng)景都需要使用到異步,因此列舉以下常見得八種異步方式
異步方式1:線程Thread
public class AsyncThread extends Thread {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("3333333333333333333 ");
}
public static void main(String[] args) {
System.out.println("111111111111111 ");
AsyncThread asyncThread = new AsyncThread();
asyncThread.start();
System.out.println("2222222222222222 ");
}
}
當(dāng)然如果每次都創(chuàng)建一個(gè)Thread線程,頻繁的創(chuàng)建、銷毀,浪費(fèi)系統(tǒng)資源,我們可以采用線程池:
可以將業(yè)務(wù)邏輯封裝到Runnable或Callable中,交由線程池來執(zhí)行。
異步方式2:Future異步
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@SuppressWarnings("ALL")
public class FutureManager {
public String execute() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<String> future = executor.submit(() -> {
System.out.println(" --- task start --- ");
Thread.sleep(3000);
System.out.println("333333333333333333333");
System.out.println(" --- task finish ---");
return "this is future execute final result!!!";
});
//這里需要返回值時(shí)會(huì)阻塞主線程
return future.get();
}
@SneakyThrows
public static void main(String[] args) {
System.out.println("1111111111111111111111");
FutureManager manager = new FutureManager();
manager.execute();
System.out.println("222222222222222222222");
}
}Future的不足之處的包括以下幾點(diǎn):
1?? 無法被動(dòng)接收異步任務(wù)的計(jì)算結(jié)果:雖然我們可以主動(dòng)將異步任務(wù)提交給線程池中的線程來執(zhí)行,但是待異步任務(wù)執(zhí)行結(jié)束之后,主線程無法得到任務(wù)完成與否的通知,它需要通過get方法主動(dòng)獲取任務(wù)執(zhí)行的結(jié)果。
2?? Future件彼此孤立:有時(shí)某一個(gè)耗時(shí)很長(zhǎng)的異步任務(wù)執(zhí)行結(jié)束之后,你想利用它返回的結(jié)果再做進(jìn)一步的運(yùn)算,該運(yùn)算也會(huì)是一個(gè)異步任務(wù),兩者之間的關(guān)系需要程序開發(fā)人員手動(dòng)進(jìn)行綁定賦予,F(xiàn)uture并不能將其形成一個(gè)任務(wù)流(pipeline),每一個(gè)Future都是彼此之間都是孤立的,所以才有了后面的CompletableFuture,CompletableFuture就可以將多個(gè)Future串聯(lián)起來形成任務(wù)流。
3?? Futrue沒有很好的錯(cuò)誤處理機(jī)制:截止目前,如果某個(gè)異步任務(wù)在執(zhí)行發(fā)的過程中發(fā)生了異常,調(diào)用者無法被動(dòng)感知,必須通過捕獲get方法的異常才知曉異步任務(wù)執(zhí)行是否出現(xiàn)了錯(cuò)誤,從而在做進(jìn)一步的判斷處理。
異步方式3:CompletableFuture
import lombok.SneakyThrows;
import java.util.concurrent.CompletableFuture;
public class CompletableFutureCompose {
/**
* thenAccept子任務(wù)和父任務(wù)公用同一個(gè)線程
*/
@SneakyThrows
public static void thenRunAsync() {
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread() + " cf1 do something....");
return 1;
});
CompletableFuture<Void> cf2 = cf1.thenRunAsync(() -> {
System.out.println(Thread.currentThread() + " cf2 do something...");
});
//等待任務(wù)1執(zhí)行完成
System.out.println("cf1結(jié)果->" + cf1.get());
//等待任務(wù)2執(zhí)行完成
System.out.println("cf2結(jié)果->" + cf2.get());
}
public static void main(String[] args) {
thenRunAsync();
}
}我們不需要顯式使用ExecutorService,CompletableFuture 內(nèi)部使用了ForkJoinPool來處理異步任務(wù),如果在某些業(yè)務(wù)場(chǎng)景我們想自定義自己的異步線程池也是可以的。
異步方式4:Spring的@Async 異步
自定義異步線程池
/**
* 線程池參數(shù)配置,多個(gè)線程池實(shí)現(xiàn)線程池隔離,@Async注解,默認(rèn)使用系統(tǒng)自定義線程池,可在項(xiàng)目中設(shè)置多個(gè)線程池,在異步調(diào)用的時(shí)候,指明需要調(diào)用的線程池名稱,比如:@Async("taskName")
@EnableAsync
@Configuration
public class TaskPoolConfig {
/**
* 自定義線程池
*
**/
@Bean("taskExecutor")
public Executor taskExecutor() {
//返回可用處理器的Java虛擬機(jī)的數(shù)量 12
int i = Runtime.getRuntime().availableProcessors();
System.out.println("系統(tǒng)最大線程數(shù) : " + i);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心線程池大小
executor.setCorePoolSize(16);
//最大線程數(shù)
executor.setMaxPoolSize(20);
//配置隊(duì)列容量,默認(rèn)值為Integer.MAX_VALUE
executor.setQueueCapacity(99999);
//活躍時(shí)間
executor.setKeepAliveSeconds(60);
//線程名字前綴
executor.setThreadNamePrefix("asyncServiceExecutor -");
//設(shè)置此執(zhí)行程序應(yīng)該在關(guān)閉時(shí)阻止的最大秒數(shù),以便在容器的其余部分繼續(xù)關(guān)閉之前等待剩余的任務(wù)完成他們的執(zhí)行
executor.setAwaitTerminationSeconds(60);
//等待所有的任務(wù)結(jié)束后再關(guān)閉線程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}需要使用異步線程池的地方
public interface AsyncService {
MessageResult sendSms(String callPrefix, String mobile, String actionType, String content);
MessageResult sendEmail(String email, String subject, String content);
}
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {
@Autowired
private IMessageHandler mesageHandler;
@Override
@Async("taskExecutor")
public MessageResult sendSms(String callPrefix, String mobile, String actionType, String content) {
try {
Thread.sleep(1000);
mesageHandler.sendSms(callPrefix, mobile, actionType, content);
} catch (Exception e) {
log.error("發(fā)送短信異常 -> ", e)
}
}
@Override
@Async("taskExecutor")
public sendEmail(String email, String subject, String content) {
try {
Thread.sleep(1000);
mesageHandler.sendsendEmail(email, subject, content);
} catch (Exception e) {
log.error("發(fā)送email異常 -> ", e)
}
}
}在實(shí)際項(xiàng)目中, 使用@Async調(diào)用線程池,推薦等方式是是使用自定義線程池的模式,不推薦直接使用@Async直接實(shí)現(xiàn)異步。
異步方式5:Spring ApplicationEvent 事件實(shí)現(xiàn)異步
定義事件
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
public class AsyncSendEmailEvent extends ApplicationEvent {
public AsyncSendEmailEvent(Object source) {
super(source);
}
/**
* 郵箱
**/
@Getter
@Setter
private String email;
/**
* 主題
**/
@Getter
@Setter
private String subject;
/**
* 內(nèi)容
**/
@Getter
@Setter
private String content;
/**
* 接收者
**/
@Getter
@Setter
private String targetUserId;
}定義異步處理
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncSendEmailEventHandler implements ApplicationListener<AsyncSendEmailEvent> {
@Autowired
private EmailService emailService;
@Async("taskExecutor")
@Override
public void onApplicationEvent(AsyncSendEmailEvent event) {
if (event == null) {
return;
}
String email = event.getEmail();
String subject = event.getSubject();
String content = event.getContent();
String targetUserId = event.getTargetUserId();
//處理異步業(yè)務(wù)
emailService.sendsendEmailSms(email, subject, content, targetUserId);
}
}另外,可能有些時(shí)候采用ApplicationEvent實(shí)現(xiàn)異步的使用,當(dāng)程序出現(xiàn)異常錯(cuò)誤的時(shí)候,需要考慮補(bǔ)償機(jī)制,那么這時(shí)候可以結(jié)合Spring Retry重試來幫助我們避免這種異常造成數(shù)據(jù)不一致問題。
異步方式6:消息隊(duì)列
回調(diào)消息的生產(chǎn)者
@Slf4j
@Component
public class CallbackProducer {
@Autowired
AmqpTemplate amqpTemplate;
public void sendCallbackMessage(CallbackDTO allbackDTO, final long delayTimes) {
log.info("生產(chǎn)者發(fā)送消息,callbackDTO,{}", callbackDTO);
amqpTemplate.convertAndSend(CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getExchange(), CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getRoutingKey(), JsonMapper.getInstance().toJson(genseeCallbackDTO), new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
//給消息設(shè)置延遲毫秒值,通過給消息設(shè)置x-delay頭來設(shè)置消息從交換機(jī)發(fā)送到隊(duì)列的延遲時(shí)間
message.getMessageProperties().setHeader("x-delay", delayTimes);
message.getMessageProperties().setCorrelationId(callbackDTO.getSdkId());
return message;
}
});
}
}回調(diào)消息的消費(fèi)者
@Slf4j
@Component
@RabbitListener(queues = "message.callback", containerFactory = "rabbitListenerContainerFactory")
public class CallbackConsumer {
@Autowired
private IGlobalUserService globalUserService;
@RabbitHandler
public void handle(String json, Channel channel, @Headers Map<String, Object> map) throws Exception {
if (map.get("error") != null) {
//否認(rèn)消息
channel.basicNack((Long) map.get(AmqpHeaders.DELIVERY_TAG), false, true);
return;
}
try {
CallbackDTO callbackDTO = JsonMapper.getInstance().fromJson(json, CallbackDTO.class);
//執(zhí)行業(yè)務(wù)邏輯
globalUserService.execute(callbackDTO);
//消息消息成功手動(dòng)確認(rèn),對(duì)應(yīng)消息確認(rèn)模式acknowledge-mode: manual
channel.basicAck((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);
} catch (Exception e) {
log.error("回調(diào)失敗 -> {}", e);
}
}
}異步方式7:ThreadUtil 異步工具類
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ThreadLocalRandom;
@Slf4j
public class ThreadUtils {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
ThreadUtil.execAsync(() -> {
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
int number = threadLocalRandom.nextInt(20) + 1;
System.out.println(number);
});
log.info("當(dāng)前第:" + i + "個(gè)線程");
}
log.info("task finish!");
}
}異步方式8:Guava異步
Guava的ListenableFuture顧名思義就是可以監(jiān)聽的Future,是對(duì)java原生Future的擴(kuò)展增強(qiáng)。我們知道Future表示一個(gè)異步計(jì)算任務(wù),當(dāng)任務(wù)完成時(shí)可以得到計(jì)算結(jié)果。如果我們希望一旦計(jì)算完成就拿到結(jié)果展示給用戶或者做另外的計(jì)算,就必須使用另一個(gè)線程不斷的查詢計(jì)算狀態(tài)。這樣做,代碼復(fù)雜,而且效率低下。使用「Guava ListenableFuture」 可以幫我們檢測(cè)Future是否完成了,不需要再通過get()方法苦苦等待異步的計(jì)算結(jié)果,如果完成就自動(dòng)調(diào)用回調(diào)函數(shù),這樣可以減少并發(fā)程序的復(fù)雜度。
public static void main(String[] args) {
//首先通過MoreExecutors類的靜態(tài)方法listeningDecorator方法初始化一個(gè)ListeningExecutorService的方法,
// 然后使用此實(shí)例的submit方法即可初始化ListenableFuture對(duì)象。
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info("callable execute...");
TimeUnit.SECONDS.sleep(1);
return 1;
}
});
//ListenableFuture要做的工作,在Callable接口的實(shí)現(xiàn)類中定義,
// 這里只是休眠了1秒鐘然后返回一個(gè)數(shù)字1,有了ListenableFuture實(shí)例,
// 可以執(zhí)行此Future并執(zhí)行Future完成之后的回調(diào)函數(shù)。
Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
@Override
public void onSuccess(Integer result) {
//成功執(zhí)行...
System.out.println("Get listenable future's result with callback " + result);
}
@Override
public void onFailure(Throwable t) {
//異常情況處理...
t.printStackTrace();
}
},Executors.newSingleThreadExecutor());
}到此這篇關(guān)于Java并發(fā)之異步的八種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)Java 異步內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java生成csv文件亂碼的解決方法示例 java導(dǎo)出csv亂碼
這篇文章主要介紹了java生成csv文件亂碼的解決方法,大家可以直接看下面的示例2014-01-01
IDEA?database和datagrip無法下載驅(qū)動(dòng)問題解決辦法
這篇文章主要給大家介紹了關(guān)于IDEA?database和datagrip無法下載驅(qū)動(dòng)問題的解決辦法,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用idea具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03
Java基礎(chǔ)之刪除文本文件中特定行的內(nèi)容
這篇文章主要介紹了Java基礎(chǔ)之刪除文本文件中特定行的內(nèi)容,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
springboot+dubbo+zookeeper的簡(jiǎn)單實(shí)例詳解
本文主要介紹了springboot+dubbo+zookeeper的簡(jiǎn)單實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Java?如何通過注解實(shí)現(xiàn)接口輸出時(shí)數(shù)據(jù)脫敏
這篇文章主要介紹了Java?如何通過注解實(shí)現(xiàn)接口輸出時(shí)數(shù)據(jù)脫敏,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java Socket編程簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java Socket編程簡(jiǎn)介的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
使用idea創(chuàng)建web框架和配置struts的方法詳解
這篇文章主要介紹了使用idea創(chuàng)建web框架和配置struts的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Dubbo服務(wù)校驗(yàn)參數(shù)的解決方案
這篇文章主要介紹了Dubbo服務(wù)如何優(yōu)雅的校驗(yàn)參數(shù),Dubbo框架本身是支持參數(shù)校驗(yàn)的,同時(shí)也是基于JSR303去實(shí)現(xiàn)的,今天通過示例代碼介紹下詳細(xì)實(shí)現(xiàn)過程,需要的朋友可以參考下2022-03-03
BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析
這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

