Service Thread Count Spikes: Tracing It to Repeated Thread Pool Creation
Monitoring showed a service's thread count spiking; troubleshooting revealed that the thread pools were being used incorrectly. This post records the full investigation and the fix, to help clarify how thread pools are supposed to be used.
Background
Monitoring flagged an abnormally high thread count in one of our services:

CPU, memory, and network connections were all normal at the same time.
Investigation
The first reaction was to look at the thread dumps:
1 | "pool-2493-thread-3" #3718833 prio=5 os_prio=0 tid=0x00007f1610041000 nid=0x38bff6 waiting on condition [0x00007f18ba29e000] |
I wanted to use the thread dumps to find where the threads were created, but all they showed was that the threads were created by a ThreadPoolExecutor — only runtime state.
What about the heap?
| Item | Value |
|---|---|
| Object/Stack Frame | java.lang.Thread @ 0x7a044af30 |
| Name | pool-2493-thread-3 |
| Shallow Heap | 120 |
| Retained Heap | 1,728 |
| Max. Locals' Retained Heap | |
| Context Class Loader | org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader @ 0x71d665028 |
| Is Daemon | false |
| State | [alive, parked, waiting, waiting indefinitely] |
| State value | 0x291 |
The heap gave me nothing useful beyond one extra field versus the thread dump: the Context Class Loader.
Going back over the thread dumps, a pattern emerged: in [pool-2493-thread-1], the number after pool keeps increasing, while the number after thread is almost always 1, 2, or 3.
From Naming Executor Service Threads and Thread Pool in Java, I inferred that somewhere in the code a thread pool was being initialized with 3 core threads. A quick search of the codebase turned up exactly such a snippet:
1 | // import cn.hutool.core.thread.ThreadUtil; |
At first I assumed the locally created threads would be reclaimed by the GC, so I forced a GC through Arthas vmtool — and the thread count did not drop.
Why weren't they reclaimed?
=>
Does an ExecutorService get garbage collected when out of scope?
=> That question mentions shutdown() on the pool, but I had not focused on it yet.
=> This led me to believe the unreclaimed threads were still reachable through some global variable, and a round of investigation found no large global thread pool anywhere.
=> Searching again eventually turned up the answer:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html
The JDK official documentation says:
1 | Finalization |
=>
A pool that is no longer referenced in the program AND has no remaining threads will be shut down automatically. If you want to ensure that unreferenced pools are reclaimed even when users forget to call shutdown(), you must arrange for unused threads to eventually die — by setting appropriate keep-alive times, using a lower bound of zero core threads, and/or setting allowCoreThreadTimeOut(boolean).
But why don't the threads get reclaimed once the pool goes out of scope?
=>
https://stackoverflow.com/questions/7728102/why-doesnt-this-thread-pool-get-garbage-collected
Conclusions
The official documentation shows there are several ways to fix this:
- Make the thread pool a member variable (recommended)
- Shut the pool down with
threadPoolExecutor.shutdown();(initiates an orderly shutdown: submitted tasks keep running, but the call doesn't block) - Enable core thread timeouts with
allowCoreThreadTimeOut(true); - Set the core thread count to 0 — but this causes other headaches in practice (omitted)
The fix is clear now. Next, a look at shutdown() and the default thread pool naming rule.
Extras
How shutdown() works
shutdown() sets the pool state to SHUTDOWN, interrupts all idle threads (idle meaning blocked on the queue), and finally sets the pool state to TERMINATED.
1 | /** |
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html#shutdown--
Default thread pool naming rule
java.util.concurrent.DefaultThreadFactory
1 | static class DefaultThreadFactory implements ThreadFactory { |
As the code shows, the default thread name follows this pattern: pool-<pool number, auto-incremented from 1>-thread-<thread number within the pool, auto-incremented from 1>
For custom thread naming, see:
https://stackoverflow.com/questions/6113746/naming-threads-and-thread-pools-of-executorservice
This article is the English edition of a post originally published in Chinese.