Open In App

How to Monitor a Thread’s Status in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

public String getName()

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

Example:

Java




// 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)
                wait();
        }
        catch (InterruptedException exc) {
            System.out.println("wait() interrupted");
        }
    }
 
    // Method 2
    synchronized void notice()
    {
        ready = true;
        notify();
    }
 
    // 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
        System.out.println("waiting...");
 
        // calling the Method1
        startWait();
 
        // Try block to check for exceptions
        try {
 
            // Making thread to pause execution for a
            // certain time of 1 second using sleep() method
            Thread.sleep(1000);
        }
 
        // 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)
    {
        System.out.println(thrd.getName()
                           + "  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
        showThreadStatus(thrd);
 
        // Starting the thread which automatically invokes
        // the run() method for the thread
        thrd.start();
 
        // Similarly repeating the same
        Thread.sleep(50);
        showThreadStatus(thrd);
 
        // here notice we change the flag value
        // that is no more in waiting state now
        thrd.waiting = false;
 
        Thread.sleep(50);
        showThreadStatus(thrd);
        thrd.notice();
 
        Thread.sleep(50);
        showThreadStatus(thrd);
 
        // Till thread is alive
        while (thrd.isAlive())
 
            // Print the statement
            System.out.println("alive");
 
        // Calling the method
        showThreadStatus(thrd);
    }
}


Output:



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