Skip to content

Instantly share code, notes, and snippets.

@arunsammit
Last active May 5, 2021 18:22
Show Gist options
  • Save arunsammit/5df352de95b4c3eb8db188cbbb39bcce to your computer and use it in GitHub Desktop.
Save arunsammit/5df352de95b4c3eb8db188cbbb39bcce to your computer and use it in GitHub Desktop.

QEats Module-8

Optimum number of threads

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

  1. 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);
  1. 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment