Java
Multithreading
Java Process
Thread Count
Software Development

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.

Browse interview questions

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.

java
1import java.lang.management.ManagementFactory;
2import java.lang.management.ThreadMXBean;
3
4public class ThreadCountDemo {
5    public static void main(String[] args) {
6        ThreadMXBean bean = ManagementFactory.getThreadMXBean();
7        System.out.println("Live threads: " + bean.getThreadCount());
8    }
9}

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.

java
System.out.println(Thread.activeCount());

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.

bash
jcmd <pid> Thread.print

or

bash
jstack <pid>

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.

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 jcmd or jstack when 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.