Get a list of all threads currently running in Java

后端 未结 13 1477
[愿得一人]
[愿得一人] 2020-11-22 02:30

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

Is it also possible to get the

相关标签:
13条回答
  • 2020-11-22 03:12

    You can use getAllThreadIds that Returns all live thread IDs. Some threads included in the returned array may have been terminated when this method returns.

    ManagementFactory.getThreadMXBean().getAllThreadIds()
    
    0 讨论(0)
  • 2020-11-22 03:13
        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 + "  ");
            }
        }
    
    0 讨论(0)
  • 2020-11-22 03:13

    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.

    0 讨论(0)
  • 2020-11-22 03:15

    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.

    0 讨论(0)
  • 2020-11-22 03:20

    Code snippet to get list of threads started by main thread:

    import java.util.Set;
    
    public class ThreadSet {
        public static void main(String args[]) throws Exception{
            Thread.currentThread().setName("ThreadSet");
            for ( int i=0; i< 3; i++){
                Thread t = new Thread(new MyThread());
                t.setName("MyThread:"+i);
                t.start();
            }
            Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
            for ( Thread t : threadSet){
                if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup()){
                    System.out.println("Thread :"+t+":"+"state:"+t.getState());
                }
            }
        }
    }
    
    class MyThread implements Runnable{
        public void run(){
            try{
                Thread.sleep(5000);
            }catch(Exception err){
                err.printStackTrace();
            }
        }
    }
    

    output:

    Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
    Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
    Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
    Thread :Thread[ThreadSet,5,main]:state:RUNNABLE
    

    If you need all threads including system threads, which have not been started by your program, remove below condition.

    if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup())
    

    Now output:

    Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
    Thread :Thread[Reference Handler,10,system]:state:WAITING
    Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
    Thread :Thread[ThreadSet,5,main]:state:RUNNABLE
    Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
    Thread :Thread[Finalizer,8,system]:state:WAITING
    Thread :Thread[Signal Dispatcher,9,system]:state:RUNNABLE
    Thread :Thread[Attach Listener,5,system]:state:RUNNABLE
    
    0 讨论(0)
  • 2020-11-22 03:25

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

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

    0 讨论(0)
提交回复
热议问题