How to Display all Threads Status in Java? Last Updated : 24 Feb, 2022 Comments Improve Suggest changes Like Article Like Report Threads are light-weight processes within a process.. Multithreading in java is a feature that allows concurrent execution of two or more parts of a program to maximize the utilization of CPU. here the approach to retrieve the state of the thread is via getState() method of the Thread class. A java thread can exist in any one of the following states, the status of a thread is the state in which it exists at a given instance. The life cycle of a thread as shown above is the best way out to learn more about the states where the states are as follows: NewRunnableBlockedWaitingTimed WaitingTerminated Note: When a thread is getting executed all other threads are in blocking state and not in waiting state. Procedure: Displaying thread status Threads are created by implementing the runnable interface.The status of a thread can be retrieved by getState() method of the Thread class object. Example: Java // Java Program to Display all Threads Status // Importing Set class from java.util package import java.util.Set; // Class 1 // helper Class implementing Runnable interface class MyThread implements Runnable { // run() method whenever thread is invoked public void run() { // Try block to check for exceptions try { // making thread to Thread.sleep(2000); } // Catch block to handle the exceptions catch (Exception err) { // Print the exception System.out.println(err); } } } // Class 2 // Main Class to check thread status public class GFG { // Main driver method public static void main(String args[]) throws Exception { // Iterating to create multiple threads // Customly creating 5 threads for (int thread_num = 0; thread_num < 5; thread_num++) { // Creating single thread object Thread t = new Thread(new MyThread()); // Setting name of the particular thread // using setName() method t.setName("MyThread:" + thread_num); // Starting the current thread // using start() method t.start(); } // Creating set object to hold all the threads where // Thread.getAllStackTraces().keySet() returns // all threads including application threads and // system threads Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); // Now, for loop is used to iterate through the // threadset for (Thread t : threadSet) { // Printing the thread status using getState() // method System.out.println("Thread :" + t + ":" + "Thread status : " + t.getState()); } } } Â Â Output:Â Comment More infoAdvertise with us Next Article How to Display all Threads Status in Java? P pulamolusaimohan Follow Improve Article Tags : Java Java Programs Java-Multithreading Practice Tags : Java Similar Reads How to Monitor a Thread's Status in Java? The Java language support thread synchronization through the use of monitors. A monitor is associated with a specific data item and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data. In order to mo 3 min read How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read How to Temporarily Stop a Thread in Java? The suspend() method of thread class puts the thread from running to waiting state. This method is employed if you would like to prevent the thread execution and begin it again when a particular event occurs. This method allows a thread to temporarily cease execution. The suspended thread is often r 2 min read How to Solve Deadlock using Threads in Java? If two threads are waiting for each other forever such type of infinite waiting is called deadlock in java. Synchronized keyword is the only reason for deadlock situation hence while using synchronized keyword we have to take special care. There is no resolution technique for deadlock, but several p 6 min read How to Check if a Thread Holds Lock on a Particular Object in Java? Java language is one of the most popular languages for many years. One of the most advantageous features of java programming is Multithreading. Multithreading allows executing a single program into many small parts of the program with the maximum utilization of the CPU. Threads are a lightweight pro 3 min read Like