#2 Asynchronous Programming in Java-Launching Several Tasks Synchronously

anil gola
3 min readJun 18, 2023

--

This blog is in continuation to my previous blog:

https://medium.com/@anil.java.story/concurrent-vs-asynchronous-16a8323c9ccf

We have precisely defined the definitions of synchronous, asynchronous, and concurrent. We should know that we should avoid blocking calls to make sure that we increase the throughput of our application with the current underlying hardware.

We need to start somewhere, and what better way to do that than to launch several tasks in a synchronous way and see how much time it takes?

The example we would be using is quotations. We would hit several quotation servers to fetch the quotation, and our aim is to find the best quotations. We would hit five quotation servers.

Callable<Quotation> fetchQuotationA = () -> {
Thread.sleep(RandomUtils.nextInt(450,500));
return new Quotation(RandomUtils.nextInt(1,100),"server-A");
};

Callable<Quotation> fetchQuotationB = () -> {
Thread.sleep(RandomUtils.nextInt(450,500));
return new Quotation(RandomUtils.nextInt(1,100),"server-B");
};

Callable<Quotation> fetchQuotationC = () -> {
Thread.sleep(RandomUtils.nextInt(450,500));
return new Quotation(RandomUtils.nextInt(1,100),"server-C");
};

Callable<Quotation> fetchQuotationD = () -> {
Thread.sleep(RandomUtils.nextInt(450,500));
return new Quotation(RandomUtils.nextInt(1,100),"server-D");
};

Callable<Quotation> fetchQuotationE = () -> {
Thread.sleep(RandomUtils.nextInt(450,500));
return new Quotation(RandomUtils.nextInt(1,100),"server-E");
};

This callable task mimics the HTTP server, where it takes nearly 450 to 500 milliseconds to respond.

We will be executing all these tasks synchronously and will see how much time it takes. First, we would collect all our tasks on some kind of list.

After that, it would be a simple classical stream question. We open a stream on taskList, execute all tasks in map operation, then find the minimum and open the optional with orElseThrow.

  Quotation bestQuotation =  taskList.stream()
.map(SynchronizedTest::executeTask)
.min(Comparator.comparing(it -> it.value)) // Optional
.orElseThrow();
 private static Quotation executeTask(Callable<Quotation> task) {
try {
return task.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

We could wrap this whole stream operations like below to gauge the time.

We would run this programme many times to see our output from the main method.

 public static void main(String[] args) throws InterruptedException {
run();
}

The results are as below after executing the programme four times.

When executing our task synchronously, we are getting the best quotations from different servers, and the time taken is roughly between 2350 ms and 2410 ms.

We have achieved something here. We need to improve from here.

Next part:

https://medium.com/@anil.java.story/approach-to-non-blocking-code-part-3-78cb1671db49

My youtube channel:

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

--

--