Open In App

How To Display All Running Threads In Java ?

Last Updated : 18 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A thread is basically a stream of instruction executed sequentially. It is used to implement multitasking in a program. A program can have multiple threads. Threads are used to do multiple things at the same time. Threads are basically used to perform complicated tasks in the background without affecting the main program.

There are two methods to display all running threads in Java

1. Using The ThreadGroup Object

Java provides us a way to group multiple threads in a single object. In Java, a group of threads ie thread groups is being implemented by ThreadGroup class, so here we will be using a ThreadGroup object to group all the threads currently running. After this, we will be using the activeCount() method of ThreadGroup to get the number of active threads, then we will use the enumerate(Thread[] list) method of the ThreadGroup which will copy the currently active threads in an array, and we will loop over the array to get the names of all the active threads.

Java




// Java Program to display all the running threads using
// ThreadGroup object
 
import java.io.*;
 
class GFGThread extends Thread {
    // overridden run method
    public void run()
    {
        System.out.println("Overridden Run Method");
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        // making a thread object
        GFGThread t1 = new GFGThread();
        GFGThread t2 = new GFGThread();
        t1.start(); // starting the thread
        t2.start();
 
        // getting the group of the threads/
        ThreadGroup threadGroup
            = Thread.currentThread().getThreadGroup();
 
        // getting the total active count of the threads
        int threadCount = threadGroup.activeCount();
 
        Thread threadList[] = new Thread[threadCount];
        // enumerating over the thread list
        threadGroup.enumerate(threadList);
 
        System.out.println("Active threads are:");
       
        // iterating over the for loop to get the names of
        // all the active threads.
        for (int i = 0; i < threadCount; i++)
            System.out.println(threadList[i].getName());
    }
}


2. Using getAllStackTrace() Method

The getAllStackTrace() method gives a stack trace of all the running threads. Then we make a set of the keys of that element because the method returns a map, and then we loop over all the elements of the set to print all the running threads.

Java




// Java Program to display all the running threads using
// getAllStackTraces() Method
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFGThread extends Thread {
   
    // overridden run method
    public void run()
    {
        System.out.println("Overridden Run Method");
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        // making a thread object
        GFGThread t1 = new GFGThread();
        GFGThread t2 = new GFGThread();
        t1.start(); // starting the thread
        t2.start();
 
        // getAllStackTraces() method will return the Set of
        // Thread Objects
        Set<Thread> threadSet
            = Thread.getAllStackTraces().keySet();
        // iterating over the threads to get the names of
        // all the active threads
        for (Thread x : threadSet) {
            System.out.println(x.getName());
        }
    }
}


 Output

Overridden Run Method
Overridden Run Method
Thread-0
Reference Handler
Thread-1
Signal Dispatcher
main
Finalizer


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads