Skip to content
Related Articles

Related Articles

How To Display All Running Threads In Java ?
  • Last Updated : 22 Feb, 2021

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 runnning threads using
// ThreadGroup object
  
import java.io.*;
  
class GFGThread extends Thread {
    // overriden run method
    public void run()
    {
        System.out.println("Overriden 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 tread
        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 runnning threads using
// getAllStackTraces() Method
import java.io.*;
import java.lang.*;
import java.util.*;
  
class GFGThread extends Thread {
    
    // overriden run method
    public void run()
    {
        System.out.println("Overriden 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 tread
        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

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :