首页  >  文章  >  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();

所以上面的代码可以使用上面代码的await方法在EA同步功能中使用;我们使用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
上一篇:Java Listiterator下一篇:Java dumps