#1 Asynchronous Programming in Java-Concurrent vs Asynchronous

anil gola
3 min readJun 6, 2023

--

There are subtle differences between the concurrent and asynchronous programming models, and in this blog, we will precisely define these terms.

First things first, “concurrent” means running your code in several threads so that more things can be done at the same time.

Synchronous: The thread that launched the task needs to wait for the task to complete before continuing to work.

Asynchronous: The task will be executed at some point in the future, and there is no need to wait for task to complete by main thread.

ExecutorService executorService =  Executors.newFixedThreadPool(3);
Future<String> weatherFuture = executorService.submit(() -> getWeatherFromServerA());
Future<Double> quotationFuture = executorService.submit(() -> getQuotationFromServerA());

weatherFuture.get(); // blocking call , // line no 4
quotationFuture.get(); // blocking call // line no 5

We have run the above two tasks in different sets of threads; we can say that concurrent execution of code is happening, but suddenly at lines 4 and 5, we have a blocking call 😮. The main thread submitted the two tasks, which will be executed at some point in the future in separate threads, but the main thread has to wait at line 4. Overall, even though this code looks like it’s fulfilling the criteria of concurrent and asynchronous, it’s actually simple concurrent but synchronous code. There is no way we could write asynchronous code prior to Java 8. Let’s write this as asynchronous code with the help of CompletableFuture

public static void main(String[] args) {
var result =
CompletableFuture.supplyAsync(Weather::getWeatherFromServerA)
.thenAcceptBoth( CompletableFuture.supplyAsync((Quotation::getQuotationFromServerA)),
(weather, quotation) -> System.out.println(weather + ":" + quotation)) ;
}

The tasks Weather::getWeatherFromServerA and Quotation::getQuotationFromServerA will be executed at some time in the future. Also, the main thread is not waiting for the result. So the code is fully asynchronous. Do the above codes run concurrently? Yes, as both tasks are running in the ForkJoin pool. Hence, it’s both concurrent and asynchronous. In CompletableFuture, we can even precisely define in which thread our task should run.

Now the question is: can a task be asynchronous and yet run on a single thread? The answer is Yes. Consider the following code snippet:

List.of("A","B","C")
.sort(Comparator.naturalOrder());

The task submitted to the sort method will be executed at some point in the future, but it will be run by the main thread. We really don’t know how many times the comparator will be called; it really depends on the underline sorting algorithm; if it is merge sort, then it will be called logarithmic of n.

Clearly, a task can be asynchronous but not concurrent. Also, a programme may be concurrent but not asynchronous, as we have seen with executor services.

CompletableFuture APIs have been built on top of the concurrent model. With these APIs, we can build concurrent asynchronous APIs.

I hope you got the gist.

Next part:

https://medium.com/@anil.java.story/approach-to-non-blocking-in-java-part-2-b1699b50e428

My youtube channel:

Asynchronous Programming : https://youtu.be/AHL2zuZ_5_k

--

--