首頁  >  文章  >  Java  >  Java異步

Java異步

WBOY
WBOY原創
2024-08-30 15:09:53826瀏覽

在Java程式語言中,async被定義為一個呼叫或一個函數,可以在另一個函數中定義,或者在另一個函數中作為參數傳遞,通常在程式需要執行而不阻塞程式時聲明,這是當呼叫從事件返回並返回到回調函數時完成。在java中,這被定義為在新建立的執行緒內呼叫的回呼方法。所以,一般來說,非同步編程無非是編寫一段非阻塞程式碼,即在單獨的執行緒上運行的程序,透過通知主執行緒執行完成或失敗的效能進度。

java 中非同步的工作

在本文中,我們將討論一種回呼方法,在java中稱為非同步函數。這個函數在java中也稱為await。在java中,透過使新執行緒本身非同步來啟動或編寫非同步程式設計。只有當任務彼此不依賴時才使用非同步回調,這可能需要一些時間來執行。因此,一般來說,非同步調用可以透過線上購物為例來解釋,當我們選擇某個商品並將其添加到購物車時,該商品將不會被阻止,因為它也可供其他人使用,而其他人則無法使用無需等待商品訂單完成。因此,每當我們想要執行任何可以在不阻塞其執行的情況下執行的程式時,都可以使用非同步程式來完成。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

在Java中,有許多編寫非阻塞程式碼的特性,例如使用執行緒、futures、流等,因為對非阻塞程式碼有很多需求,需要非同步程式設計來非同步執行程式碼。那麼讓我們來討論一下jcabi方面、cactoos、guava、completeableFutures、FutureTask、EA Async等幾種實現非同步程式設計的方法

1.完整的期貨

completeableFutures 是 JavaScript Promise 的 java 版本,稱為completeableFutures,它可以實現兩個接口,例如 Future 和 CompletionStage,這兩個接口的組合完成了此功能,用於編寫或使用異步編程。此功能提供了許多方法,例如supplyAsync、runAsync等。這些方法用於啟動程式碼的非同步部分,因為supplyAsync方法是在我們對結果做某事時使用的,如果我們不想要任何東西,我們可以使用runAsync 方法。 CompleteableFutures 中還有其他不同的方法,例如thenCompose 如果我們想要一個接一個地使用多個completeableFutures,或者簡單地當我們想要使用嵌套的completeableFutures並且如果我們想要組合兩個completeableFutures的結果,那麼有一個名為thenCombine方法的方法。因此,所有這些方法都在一個可完成的未來中處理,而未來又具有保存所有這些方法的完成階段方法。

範例:使用無參數建構函式建立completeableFuture,語法如下:

CompleteableFuture <String> completeableFuture = new CompleteableFuture <String>();

所以要取得結果,我們必須使用 get() 方法。所以我們可以寫成

String result =completeableFuture.get() 其中此 gets() 方法將阻塞直到 Future 完成,但此呼叫將永遠阻塞它,因為 Future 永遠不會完成。所以我們必須透過呼叫下面的方法手動完成。

completeableFuture.complete("Result")

因此,客戶端會得到指定的結果,而忽略後續呼叫。該程式可能如下所示。

CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
while (!completableFuture.isDone()) {
System.out.println("CompletableFuture is not finished yet...");
}
long result = completableFuture.get();

2. EA 非同步

這是java中用於順序編寫非同步程式碼的另一個特性,它自然提供了簡單的程式設計和擴充。這是 Electronic Arts,它獲得了透過 ea-async 庫賦予 java 生態系統的非同步等待功能。此功能轉換運行時程式碼並重寫對await 方法的調用,其工作方式與completableFuture 類似。因此,我們可以透過呼叫 Async.init 方法來初始化 Async 運行時,使用 EA-sync 方法(稱為 wait 方法)來實作上述completeableFuture 程式碼。

因此,讓我們考慮一個使用completeableFuture 和 EA 同步進行階乘的範例。

CompletableFuture <Double> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
while (!completableFuture.isDone()) {
System.out.println("The completeableFuture is not completed...");
}
double res = completableFuture.get();

所以上面的程式碼可以在EA同步功能中使用上面程式碼的await方法;我們使用await()方法而不是get()方法,因此上面的程式碼可以只用最後一行更新,而且我們必須在靜態區塊中透過Async的init方法進行初始化,其餘程式碼保持不變。

static { Async.init(); }
public func_name(){….. same as above code of completeableFuture…
double res Async.await(completableFuture);

From the above sample code, which is transformed code of completeableFuture code by using static block also for initializing the Async runtime so that the Async can transform the code of completeableFuture code during runtime and then to the await method, it can rewrite the calls which will now EA async will behave similarly to that of using the chain of completeableFuture or Future.join method. So now, when once the asynchronous execution of any method is completed, then the result from the Future method is passed to another method where the method is having the last execution using the CompleteableFuture.runAsync method.

In Java, as discussed above, there are many different ways for writing the asynchronous programming using various other methods.

Conclusion

In this article, we discussed java async where it is defined as a callback method that will continue the execution of the program without blocking by returning the calls to the callback function. This article saw how asynchronous programming is written in java using different features such as CompleteableFutures, EA async, Future Task, Guava, etc. In this article, we have seen two among these features for making the callback functions asynchronous by using various methods provided by the given respective features.

以上是Java異步的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn