Get a list of all threads currently running in Java

JavaMultithreadingJvm

Java Problem Overview


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 Thread and Class objects of all threads in the list?

I want to be able to do this through code.

Java Solutions


Solution 1 - Java

To get an iterable set:

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

Performance: 0 ms for 12 threads (Azul JVM 16.0.1, Windows 10, Ryzen 5600X).

Solution 2 - Java

Get a handle to the root ThreadGroup, like this:

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:

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.

Solution 3 - Java

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:

kill -3 processid

and the VM will do a thread dump to stdout.

Solution 4 - Java

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.

Solution 5 - Java

Have you taken a look at jconsole?

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

You can start jconsole from the JDK bin folder.

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

Solution 6 - Java

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.

Solution 7 - Java

Apache Commons users can use ThreadUtils. The current implementation uses the walk the thread group approach previously outlined.

for (Thread t : ThreadUtils.getAllThreads()) {
      System.out.println(t.getName() + ", " + t.isDaemon());
}

Solution 8 - Java

In Groovy you can call private methods

// 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.

Solution 9 - Java

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

Solution 10 - Java

To get a list of threads and their full states using the terminal, you can use the command below:

jstack -l <PID>

Which <PID> is the id of process running on your computer. To get the process id of your java process you can simply run the jps command.

Also, you can analyze your thread dump that produced by jstack in TDAs (Thread Dump Analyzer) such fastthread or spotify thread analyzer tool.

Solution 11 - Java

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.

Solution 12 - Java

    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 + "  ");
        }
    }

Solution 13 - Java

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrytenView Question on Stackoverflow
Solution 1 - JavathejoshwolfeView Answer on Stackoverflow
Solution 2 - JavaFrerich RaabeView Answer on Stackoverflow
Solution 3 - JavacletusView Answer on Stackoverflow
Solution 4 - JavaDan DyerView Answer on Stackoverflow
Solution 5 - JavapjpView Answer on Stackoverflow
Solution 6 - JavahasskellView Answer on Stackoverflow
Solution 7 - JavagerardwView Answer on Stackoverflow
Solution 8 - JavaJarek PrzygódzkiView Answer on Stackoverflow
Solution 9 - JavaRavindra babuView Answer on Stackoverflow
Solution 10 - JavaAmir FoView Answer on Stackoverflow
Solution 11 - JavaraoulssonView Answer on Stackoverflow
Solution 12 - JavaZZ CoderView Answer on Stackoverflow
Solution 13 - JavaMartin KerstenView Answer on Stackoverflow