
The former one causes CPU to spend too much time for thread switching instead of a real task, also causes too much memory usage problems, the latter one causes idle CPU while we have tasks that should be processed. The point is selecting not too much and not too small. We need a thread pool size for the newFixedThreadPool(int nThreads), and we should select ideal size to increase our application throughput and responsiveness(I assume that we have tasks that can be run independently). And again this causes a decrease in our application responsiveness and throughput. If we create threads above than a threshold then we can’t use CPU resource efficiently because CPU spends most of its time with thread or context switching other than the real work. If our tasks are not short living, using this kind of thread pool can cause creating many threads on the application. This pool usage can be meaningful for our independent tasks for some scenarios, such as if our tasks are short living tasks. We also don’t need a pool size for newCachedThreadPool(), because this pool creates a new thread or uses an existing thread for each task that’s submitted to the pool. We don’t need a pool size for newSingleThreadExecutor(), because there is only 1 thread for this pool, therefore every task that we submit to this thread pool is working sequentially, there is no concurrency and if we have tasks that can run independently, this configuration is not good in terms of our application throughput and responsiveness.

With the usage of thread pools, we aim to run these individual tasks concurrently with using system resources efficiently. To achieve this, firstly we should divide our application work into independent tasks and then we should run these tasks with efficient usage of system resources like CPU, RAM (Utilization).

We expect both a good throughput and good responsiveness from our applications.

This system workload should be tasks that can run independently, for example, every Http request to a web application can be in this category, we can process every request without thinking another Http request. We use thread pools to align our system workload with system resources efficiently. And how these parameters are affected when we run our application with Docker and Kubernetes. In this post, I explain why we use JVM thread pools, what are the parameters that we should take into account when we decide a thread pool and it’s size. Finding Ideal JVM Thread Pool Size With Kubernetes and Docker Mon, Jan 27, 2020
