Open In App
Related Articles

How to Monitor a Thread’s Status in Java?

Like Article
Save Article
Report issue

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 monitor a thread’s status Java have predefined currentThread.getName() method that is extended by Thread Class.The getName() method of java.lang.reflect.Method class is used to get the name of the entity, as a String, that entity can be class, interface, array, enum, method, etc. of the class object. The getName() method of java.lang.reflect. Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects.


public String getName()

Return Value: It returns the name of the method, as String.



// Java Program to Monitor a Thread's Status
// Class 1
// Helper class
class MyThread extends Thread {
    // Initially initializing states using boolean methods
    boolean waiting = true;
    boolean ready = false;
    // Constructor of this class
    MyThread() {}
    // Methods of this class are as follows:
    // Method 1
    synchronized void startWait()
        try {
            while (!ready)
        catch (InterruptedException exc) {
            System.out.println("wait() interrupted");
    // Method 2
    synchronized void notice()
        ready = true;
    // Method 3
    // To run threads when called using start()
    public void run()
        // Getting the name of current thread
        // using currentThread() and getName() methods
        String thrdName = Thread.currentThread().getName();
        // Print the corresponding thread
        System.out.println(thrdName + " starting.");
        // While the thread is in waiting state
        while (waiting)
            System.out.println("waiting:" + waiting);
        // Display message
        // calling the Method1
        // Try block to check for exceptions
        try {
            // Making thread to pause execution for a
            // certain time of 1 second using sleep() method
        // Catch block to handle the exceptions
        catch (Exception exc) {
            // Display if interrupted
            System.out.println(thrdName + " interrupted.");
        // Else display the thread is terminated.
        System.out.println(thrdName + " terminating.");
// Class 2
// Main class
public class GFG {
    // Method 1
    // To get the thread status
    static void showThreadStatus(Thread thrd)
                           + "  Alive:=" + thrd.isAlive()
                           + " State:=" + thrd.getState());
    // Method 2
    // Main driver method
    public static void main(String args[]) throws Exception
        // Creating an object of our thread class
        // in the main() method
        MyThread thrd = new MyThread();
        // Setting the name for the threads
        // using setname() method
        thrd.setName("MyThread #1");
        // getting the status of current thread
        // Starting the thread which automatically invokes
        // the run() method for the thread
        // Similarly repeating the same
        // here notice we change the flag value
        // that is no more in waiting state now
        thrd.waiting = false;
        // Till thread is alive
        while (thrd.isAlive())
            // Print the statement
        // Calling the method



Last Updated : 06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads