How to get the number of threads in a Java process
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There are two slightly different questions here: how many threads your current Java code sees from inside the JVM, and how many operating-system threads the Java process is using from the outside. The right tool depends on which view you actually need.
Use ThreadMXBean Inside the JVM
The standard programmatic way is ThreadMXBean.
This gives you the current live thread count seen by the JVM management layer.
Know What Thread.activeCount() Means
You may also see Thread.activeCount(), but it is a rough estimate for the current thread group, not the best general answer for process-wide monitoring.
It is fine for quick experiments, but ThreadMXBean is usually the clearer and more complete tool.
Use JDK Tools from Outside the Process
If you are diagnosing a running application from the command line, JDK tools are often more practical than code changes.
or
These tools let you inspect thread state and count without modifying the application. They are especially useful in production debugging and incident response.
OS-Level Counts Can Differ Slightly in Meaning
From the operating system side, you can also inspect the process with system tools. On Linux, for example, ps -L -p <pid> can show thread-level information.
That view is useful when correlating JVM behavior with OS scheduling, but remember that the JVM view and the OS view are not always framed with the same terminology or timing.
Count Threads for Monitoring, Not Just Curiosity
Thread count becomes valuable when:
- a service leaks threads over time
- request latency grows under load
- deadlocks or blocked executors are suspected
- a thread pool is misconfigured
The number alone is not the whole diagnosis, but it is a useful signal when combined with thread dumps and pool metrics.
Avoid Confusing Thread Count with Pool Size
A Java process can have:
- application worker threads
- GC threads
- JIT compiler threads
- framework-managed thread pools
- monitoring or signal threads
That is why "how many threads exist?" and "how many request workers do I have?" are different questions.
Count Trends Are More Useful Than One Snapshot
A single thread count can be interesting, but a rising count over time is usually the stronger signal of a leak or misconfiguration. Monitoring systems are most useful when they show change, not just one number.
That is why dashboards beat ad hoc sampling.
Trend lines tell more.
They reveal leaks.
Clearly.
Common Pitfalls
- Using
Thread.activeCount()as if it were an exact process-wide metric. - Looking only at the count without examining thread states or stack traces.
- Forgetting that JVM internal threads also contribute to the total.
- Confusing executor pool size with the total number of threads in the process.
- Adding custom thread-count code when JDK diagnostic tools would answer the question faster.
Summary
- Use
ThreadMXBean.getThreadCount()for the standard in-process answer. - Treat
Thread.activeCount()as a rough estimate, not the main diagnostic tool. - Use
jcmdorjstackwhen inspecting a live process from outside. - Remember that total thread count includes JVM internal threads too.
- Pair the count with thread dumps and pool metrics for real diagnosis.
Related reading
- How to get the return value from a thread?
- How to get the return value from a thread?
- How to get the ThreadPoolExecutor to increase threads to max before queueing?
- How to get thread id from a thread pool?
- how to get the one entry from hashmap without iterating
- How to get the path of a running JAR file?
- How to give priority to privileged thread in mutex locking?
- How to handle async Start errors in TopShelf

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.