3 Thread Pool Pitfalls You Must Avoid as a Developer

Gejiufelix
Javarevisited
Published in
3 min readAug 27, 2022

--

  1. Too many threads are created in the thread pool, resulting in OOM

For most of my friends, they have thought about a problem. If the thread pool newFixedThreadPool created by the Executors component uses an unbounded queue, it may cause OOM. Then, the Executors component can also create other thread pools, such as newCachedThreadPool, is this feasible?
Here, we need to understand that the maximum number of threads for a thread is Integer.MAX_VALUE. Seeing this, I think everyone should realize that using it may cause any problems. Yes, as everyone thinks, if a large number of threads are created, it may also cause OOM!

From the above, we can conclude that when we use the thread pool, we need to pay attention to the creation of too many threads, which leads to OOM problems.

So try not to use newCachedThreadPool, and if you customize the thread pool, you must pay attention to the maximum number of threads.
Having said so much, let’s make a comment here. Let’s take a look at the constructor of newCachedThreadPool:

public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,

--

--