CompletableFuture deals with asynchronous programming. It run the process asynchronously. It allows the programs to execute in parallelly.
Built in package: java.util.concurrent.
Why completableFuture class?
CompletableFuture is efficient and provide response in
timely manner. It has many functions listed below.
- supplyAsync ,runAsync : It deals with asynchronous computations to run the tasks.
- thenApply,thenAccept,thenRun : Multiple tasks executed and linked together to perform the computation.
- thenCombine,thenCompose,allOf : It combines the tasks.
- Exception handling: If any error occurs, this will handle.
Let the program begins.
First, Asynchronous Computation using completableFuture Program
in java is given below.
Steps:
- · Create a public class CFEg with main() function.
- · Next, include the lambda expression to execute the code.
- · To print the output, use try and catch block to avoid errors.
//Program to implement Asynchronous computation using
CompletableFuture.
import java.util.concurrent.*;
public class CFEg {
public static
void main(String[] args) {
CompletableFuture<String>
fu1 = CompletableFuture.supplyAsync(() -> {
return
"Hi,Asynchronous Computation is done with completableFuture";
});
try {
System.out.println(fu1.get());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Save,compile and execute the program to get the output.
C:\raji\blog>javac CFEg.java
C:\raji\blog>java CFEg
Hi,Asynchronous Computation is done with
completableFuture
Next program is to implement the chaining tasks.
Program to implement chaining tasks using
CompletableFuture:
Steps:
- · Create a class and main() function.
- · Create three objects for CompletabeFuture. Two for getting input as chaining tasks. Third one for combining these two.
- · Print the output using try and catch block.
//Program to implement chaining tasks using
CompletableFuture:
import java.util.concurrent.*;
public class CFEg1 {
public static void main(String[] args) {
CompletableFuture<String> fut1 =
CompletableFuture.supplyAsync(() -> "Welcome to the world of");
CompletableFuture<String> fut2 =
CompletableFuture.supplyAsync(() -> "CompletableFuture coding");
CompletableFuture<String> combinedFuture = fut1.thenCombine(fut2,
(ts1, ts2) -> ts1 + " " + ts2);
try {
System.out.println(combinedFuture.get());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Compile and execute the program.
C:\raji\blog>javac CFEg1.java
C:\raji\blog>java CFEg1
Welcome to the world of CompletableFuture coding
Next program is given below.
Program to implement combining future using
CompletableFuture:
Steps:
- · Import the built in package.
- · Create a class and include main function.
- · Using the system functions to print the output.
//Program to implement combining future using
CompletableFuture:
import java.util.concurrent.*;
public class CFEg2 {
public static void
main(String[] args) {
try {
CompletableFuture<Void> fut =
CompletableFuture.supplyAsync(() -> "Asynchronous")
.thenAcceptAsync(res -> System.out.println(res + " Programming"));
fut.get();
} catch(Exception e)
{
System.out.println(e);
}
}
}
Run the program and the output is displayed.
C:\raji\blog>javac CFEg2.java
C:\raji\blog>java CFEg2
Asynchronous
Programming
These are the different programs to implement CompletableFuture class in java8. It is useful in building efficient code.
No comments:
Post a Comment