获取当前在 Java 中运行的所有线程的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1323408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:15:54  来源:igfitidea点击:

Get a list of all threads currently running in Java

javamultithreadingjvm

提问by Kryten

Is there any way I can get a list of all running threads in the current JVM (including the threads notstarted by my class)?

有什么方法可以获取当前 JVM 中所有正在运行的线程的列表(包括我的类启动的线程)?

Is it also possible to get the Threadand Classobjects of all threads in the list?

是否也可以获取列表中所有线程的ThreadClass对象?

I want to be able to do this through code.

我希望能够通过代码做到这一点。

采纳答案by thejoshwolfe

To get an iterable set:

要获得可迭代集:

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

回答by cletus

Yes, take a look at getting a list of threads. Lots of examples on that page.

是的,看看获取线程列表。该页面上有很多示例。

That's to do it programmatically. If you just want a list on Linux at least you can just use this command:

那就是以编程方式做到这一点。如果您至少只想要 Linux 上的列表,则可以使用以下命令:

kill -3 processid

and the VM will do a thread dump to stdout.

并且虚拟机将线程转储到标准输出。

回答by pjp

Have you taken a look at jconsole?

你看过jconsole吗?

This will list all threads running for a particular Java process.

这将列出为特定 Java 进程运行的所有线程。

You can start jconsole from the JDK bin folder.

您可以从 JDK bin 文件夹中启动 jconsole。

You can also get a full stack trace for all threads by hitting Ctrl+Breakin Windows or by sending kill pid --QUITin Linux.

您还可以通过Ctrl+Break在 Windows 中点击或kill pid --QUIT在 Linux 中发送来获取所有线程的完整堆栈跟踪。

回答by Frerich Raabe

Get a handle to the root ThreadGroup, like this:

获取 root 的句柄ThreadGroup,如下所示:

ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parentGroup;
while ((parentGroup = rootGroup.getParent()) != null) {
    rootGroup = parentGroup;
}

Now, call the enumerate()function on the root group repeatedly. The second argument lets you get all threads, recursively:

现在,enumerate()重复调用根组上的函数。第二个参数让你递归地获取所有线程:

Thread[] threads = new Thread[rootGroup.activeCount()];
while (rootGroup.enumerate(threads, true ) == threads.length) {
    threads = new Thread[threads.length * 2];
}

Note how we call enumerate() repeatedly until the array is large enough to contain all entries.

请注意我们如何重复调用 enumerate() 直到数组足够大以包含所有条目。

回答by raoulsson

In the java console, hit Ctrl-Break. It will list all threads plus some information about the heap. This won't give you access to the objects of course. But it can be very helpful for debugging anyway.

在 Java 控制台中,点击Ctrl-Break。它将列出所有线程以及有关堆的一些信息。这当然不会让您访问对象。但无论如何它对调试非常有帮助。

回答by Dan Dyer

You can get a lot of information about threads from the ThreadMXBean.

您可以从ThreadMXBean获得很多关于线程的信息。

Call the static ManagementFactory.getThreadMXBean()method to get a reference to the MBean.

调用静态ManagementFactory.getThreadMXBean()方法以获取对 MBean 的引用。

回答by ZZ Coder

    public static void main(String[] args) {


        // Walk up all the way to the root thread group
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup parent;
        while ((parent = rootGroup.getParent()) != null) {
            rootGroup = parent;
        }

        listThreads(rootGroup, "");
    }


    // List all threads and recursively list all subgroup
    public static void listThreads(ThreadGroup group, String indent) {
        System.out.println(indent + "Group[" + group.getName() + 
                ":" + group.getClass()+"]");
        int nt = group.activeCount();
        Thread[] threads = new Thread[nt*2 + 10]; //nt is not accurate
        nt = group.enumerate(threads, false);

        // List every thread in the group
        for (int i=0; i<nt; i++) {
            Thread t = threads[i];
            System.out.println(indent + "  Thread[" + t.getName() 
                    + ":" + t.getClass() + "]");
        }

        // Recursively list all subgroups
        int ng = group.activeGroupCount();
        ThreadGroup[] groups = new ThreadGroup[ng*2 + 10];
        ng = group.enumerate(groups, false);

        for (int i=0; i<ng; i++) {
            listThreads(groups[i], indent + "  ");
        }
    }

回答by Jarek Przygódzki

In Groovyyou can call private methods

Groovy 中,您可以调用私有方法

// Get a snapshot of the list of all threads 
Thread[] threads = Thread.getThreads()

In Java, you can invoke that method using reflection provided that security manager allows it.

Java 中,如果安全管理器允许,您可以使用反射调用该方法。

回答by hasskell

You can try something like this:

你可以尝试这样的事情:

Thread.getAllStackTraces().keySet().forEach((t) -> System.out.println(t.getName() + "\nIs Daemon " + t.isDaemon() + "\nIs Alive " + t.isAlive()));

and you can obviously get more thread characteristic if you need.

如果需要,您显然可以获得更多线程特性。