[Java] Invoke multiple functions async

[Java] Invoke multiple functions async

Asynchronous programming in Java allows a program to perform multiple tasks simultaneously without blocking the execution of other tasks. This is achieved by using threads, where a task is executed on a separate thread, allowing other tasks to continue running on the main thread.

Java provides the Executor framework for asynchronous programming, which simplifies the management of threads. The Executor interface provides a way to run a task asynchronously, and the ExecutorService interface provides additional features such as the ability to submit multiple tasks, schedule tasks for future execution, and shut down the executor.

Java also provides the CompletableFuture class for handling asynchronous computations. This class allows you to create a promise for a computation that will be executed in the future, and provides a way to attach callbacks to be executed when the computation is complete.

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

Java also provides the AsynchronousSocketChannel and AsynchronousServerSocketChannel classes for asynchronous I/O operations. These classes allow you to perform I/O operations such as read and write asynchronously, which can greatly improve the performance of network-based applications.

Java 9 introduced Flow API which is a Reactive Streams implementation, it makes it easier to write code that can handle streams of data, handle backpressure and work with asynchronous data sources.