I found out that 2 threads are optimum in this case. I tried playing around with number of threads using the variables
numThreads = 2
executor.setCorePoolSize(numThreads);
executor.setMaxPoolSize(numThreads);
When I kept the numThreads to 4, (maybe due to i/o bottle neck or due to number of cores in my pc being 2), the response time was worse compared to the synchronous code. keeping it as 2 made the asynchronous implementation fast enough to beat get lower response time compared t o the synchronous case
How to debug multithreaded applications and make sure that your code is indeed running asynchronously
- log start time and time taken for each task/method that is being parallelized. After this check for the start time of each parallelized method (if they are almost equal then it denotes that the methods are being run parallely)
long startTime = System.currentTimeMillis();
//some code in the parallelized method
log.info("{} Found out restaurants by Item Name {}",startTime, System.currentTimeMillis()-startTime);
- Introduce artificial delays to get rid of bottlenecks like disk read write i/o operations in the methods which are being parallelized
Thread.sleep(1000L)