How To Display All Running Threads In Java ?

18 Apr, 2022
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
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());    }}
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() Methodimport 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
rs1686740
adnanirshad158
simmytarika5
rkbhola5
Java-Multithreading
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Strings in Java
Different ways of Reading a text file in Java
Reverse an array in Java
How to remove an element from ArrayList in Java?
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Traverse Through a HashMap in Java
Extends vs Implements in Java