Get All Running JVM Threads – 获取所有正在运行的JVM线程

最后修改: 2021年 9月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll learn how to get all running threads in the current JVM, including the threads not started by our class.

在这个简短的教程中,我们将学习如何获取当前JVM中所有正在运行的线程,包括不是由我们的类启动的线程。

2. Use the Thread Class

2.使用Thread

The getAllStackTrace() method of the Thread class gives a stack trace of all the running threads. It returns a Map whose keys are the Thread objects, so we can get the key set and simply loop over its elements to get information about the threads.

Thread类的getAllStackTrace()方法给出了所有运行线程的堆栈跟踪。它返回一个Map,其键值是Thread对象,所以我们可以获得键值集,并简单地在其元素上循环以获得线程的信息。

Let’s use the printf() method to make the output more readable:

让我们使用printf()方法来使输出更加可读。

Set<Thread> threads = Thread.getAllStackTraces().keySet();
System.out.printf("%-15s \t %-15s \t %-15s \t %s\n", "Name", "State", "Priority", "isDaemon");
for (Thread t : threads) {
    System.out.printf("%-15s \t %-15s \t %-15d \t %s\n", t.getName(), t.getState(), t.getPriority(), t.isDaemon());
}

The output will look like this:

输出将看起来像这样。

Name            	 State           	 Priority        	 isDaemon
main            	 RUNNABLE        	 5               	 false
Signal Dispatcher 	 RUNNABLE        	 9               	 true
Finalizer       	 WAITING         	 8               	 true
Reference Handler 	 WAITING         	 10              	 true

As we see, besides thread main, which runs the main program, we have three other threads. This result may vary with different Java versions.

正如我们所看到的,除了运行主程序的线程main,我们还有其他三个线程。这个结果可能因不同的Java版本而不同。

Let’s learn a bit more about these other threads:

让我们多了解一下这些其他的线程。

  • Signal Dispatcher: this thread handles signals sent by the operating system to the JVM.
  • Finalizer: this thread performs finalizations for objects that no longer need to release system resources.
  • Reference Handler: this thread puts objects that are no longer needed into the queue to be processed by the Finalizer thread.

All these threads will be terminated if the main program exits.

如果主程序退出,所有这些线程将被终止。

3. Use the ThreadUtils Class from Apache Commons

3.使用Apache Commons的ThreadUtils

We can also use the ThreadUtils class from Apache Commons Lang library to achieve the same goal:

我们也可以使用Apache Commons Lang库中的ThreadUtils类来实现同样的目标。

Let’s add a dependency to our pom.xml file:

让我们在我们的pom.xml文件中添加一个依赖项。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.10</version>
</dependency>

And simply use the getAllThreads() method to get all running threads:

并简单地使用getAllThreads()方法来获得所有正在运行的线程。

System.out.printf("%-15s \t %-15s \t %-15s \t %s\n", "Name", "State", "Priority", "isDaemon");
for (Thread t : ThreadUtils.getAllThreads()) {
    System.out.printf("%-15s \t %-15s \t %-15d \t %s\n", t.getName(), t.getState(), t.getPriority(), t.isDaemon());
}

The output is the same as above.

其输出结果与上述相同。

4. Conclusion

4.总结

In summary, we’ve learned two methods to get all running threads in the current JVM.

综上所述,我们已经学会了两种方法来获取当前JVM中的所有运行线程