
Now the question gets to be, does it make sense to have more than 4? This seems evident to us as if we have more CPU available and we use less thread, which means we are basically wasting our CPU. That means we have to have at least 4 threads. Let's assume we have a machine with 4 CPUs our goal is to maximize the usages of that 4 CPUs.

It also depends on how often the individual task will block. It depends on the nature of the workload we are putting in and the hardware the application is running. So the first question would be, what would be the maximum number? Well, the answer isn't straightforward. If we put too many, that may result in detrimental to the application's performance. However, the question still remains: what would be the minimum and the maximum number that we should use? Because this very critical to get the best performance. The ThreadPoolExecutor has multiple constructors and the Executors factory class uses the combination of these constructors and creates ThreadPool for us and makes our life a bit easy.

If we submit a task, these existing threads will pick it up. In the above ThreadPool, the minimum threads count is 5, which is when we would create the Pool, 5 threads will be created immediately, and they will be kept around, waiting for tasks, even if they are idle. Var threadPool = new ThreadPoolExecutor(5, 25, 100, TimeUnit.MILLISECONDS, new LinkedBlockingDeque(),ĮfaultThreadFactory(), new AbortPolicy()) įor (int i = 0 i ("Hello \uD83C\uDF0E"))

Java Thread Programming (Part 13) February 08, 2022
