Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet201/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   197   198   199   200   201   202   203   204   ...   245
Bog'liq
Java 17 Recipes

 How It Works
CompletableFuture was added in Java 8 to support asynchronous tasks. 
CompletableFuture is an extension of Future, which adds many methods to 
promote asynchronous, event-driven programming models, and also allows for values to 
be set at any time. The latter functionality means that a CompletableFuture object can 
be created before it is required if an application needs to use it in the future.
There are a couple of options for creating a CompletableFuture object, either 
manually or via factory methods. Manual creation can be done without binding to 
any thread. Such a tactic is useful when an application requires a placeholder for 
an event that occurs in the future. The following code demonstrates how to create a 
CompletableFuture object manually.
final completableFuture = new 
CompletableFuture<>();
One would utilize a factory to generate a CompletableFuture to return an object 
geared toward a specific task or outcome. There are several different factory methods 
to call on to return such an object. Some of the factory methods accept arguments, and 
others do not. For instance, the CompletableFuture.runAsync(Runnable) method 
returns a CompletableFuture that first executes the provided Runnable and then 
asynchronously completes it by a task running in the ForkJoinPool.commonPool(). 
Another variation of the runAsync() method accepts both Runnable and Executor
which first executes the provided Runnable, then asynchronously completes by a task 
within the given Executor.
The CompletableFuture object also contains several methods similar to that 
of the standard Future object. For instance, the isDone(), cancel(), and 
isCompletedExceptionally() methods each return boolean to indicate a status on the 
object. It is also possible to stack asynchronous tasks with a CompletableFuture object 
by calling on the thenApply() method, which accepts lambda expressions and method 
references. The solution to this recipe demonstrates how to utilize the thenApply() 
method to invoke an asynchronous task from another. First, a CompletableFuture object 
named performWork() is executed, then a lambda expression is executed, creating a 
concatenated string based on the string generated within performWork(). Once the 
second task has been completed, another task is invoked to append more text to the string. 
The future.get() method is then called within a loop to see the string being transformed 
by the application over time. Lastly, the outcome of the fully completed task is printed.
Chapter 10 ConCurrenCy


395
10-12. Summary
It is important to understand the fundamentals of concurrency when developing 
applications. There is nothing worse than testing an application successfully and then 
having it fail with a deadlock once it is released into production. This chapter started 
with the basics, demonstrating how to spawn a background task.
Chapter 10 ConCurrenCy


397
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_11

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   197   198   199   200   201   202   203   204   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling