In an asynchronous process, the programs and the operation can run in parallel. Therefore, it uses multiple requests to send it over to the server. The tasks in asynchronous processing run in parallel with the main tasks. These tasks are completed and reported back when needed. This write-up will specifically illustrate the working of Future and Callable for Asynchronous processing in Java.
How to Use Java Callable and Future for Asynchronous Processing
Callable and Future are categorized under asynchronous processing. The callable method in Java helps to run the instance of a class through a thread. The future is responsible for saving the results that have been obtained from other threads. The methods in the future are responsible for the computation, retrieval, and completion of results.
Example: Implementation of Java Callable and Future for Asynchronous Processing
In the code below we have implemented the callable and future in Java.
import java.util.concurrent.*;
class FutureandCallable
{
public static void main(String args[])
{
//creating an executor service with a pool of 5 threads.
ExecutorService exe = Executors.newFixedThreadPool(5);
//submitting 5 tasks for execution
Future<String> ft1 = exe.submit(new Print("DataScience"));
Future<String> ft2 = exe.submit(new Print("IT"));
Future<String> ft3 = exe.submit(new Print("Cybersecurity"));
Future<String> ft4 = exe.submit(new Print("Database"));
//
try
{
//Using the get() method to obtain the Future Value.
System.out.println("1. " + ft1.get());
System.out.println("2. " + ft2.get());
while(!ft3.isDone())
{
//If the task is running, wait for it to finish.
System.out.println("waiting for task to finish");
Thread.sleep(10);
}
System.out.println("3. " + ft3.get());
System.out.println("4. " + ft4.get());
}
//Exception statement.
catch (InterruptedException | ExecutionException e)
{
//print in case of any exception
e.printStackTrace();
}
//This method will shut down the executor service.
exe.shutdown();
}
}
//Implementing callable Interface
class Print implements Callable <String>
{
//Declare the statement to print the string
String str1;
Print(String str1)
{
this.str1= str1;
}
//The override method
@Override
public String call() throws Exception
{
//The output statement for the Strings in the future interface that uses the call() method
System.out.println("The call method is " + str1);
//The string buffer to modify any objects if needed.
StringBuffer s = new StringBuffer();
//Return the values respectively
return (s.append("Length of string ").append(str1).append("=").append(str1.length())).toString();
}
}
In the above code:
- The executor service from the thread pool executor class is called to declare the number of threads.
- The submit() method is employed to submit the tasks to the thread pool for execution/implementation.
- The get() method is responsible for fetching the element from the array.
- The executor service that has been previously called is terminated by invoking the shutdown() method.
- The callable interface has been implemented and the string has been passed.
- The call() method throws an exception.
- The string saved in the callable interface is printed on the screen.
- In order to modify, the string buffer is available.
- The size of the string is displayed by utilizing the println() method.
Output
In the below output the call() method of the callable interface has returned the value that was entered as the input. The task that contains the string as Database is made to sleep. It prints the output when it wakes up. The length of the string entered is printed accordingly.

Conclusion
The Callable and Future in Java are used for asynchronous processing. The call() method is responsible for the execution of given tasks and output or the value is available through the future. This article has demonstrated in detail the implementation/use of Java callables and futures for asynchronous processing.