📜  如何在Java监控线程的状态?

📅  最后修改于: 2022-05-13 01:54:49.690000             🧑  作者: Mango

如何在Java监控线程的状态?

Java语言通过使用监视器来支持线程同步。监视器与特定数据项相关联,并用作该数据的锁。当一个线程持有某些数据项的监视器时,其他线程被锁定并且无法检查或修改数据。为了监控线程的状态, Java预定义了currentThread.getName()方法,该方法由 Thread 类扩展Java.lang.reflect.Method 类的getName() 方法用于获取实体的名称,作为字符串,该实体可以是类对象的类、接口、数组、枚举、方法等。 Ĵava.lang.reflectgetName()方法。方法类有助于获取方法的名称,作为字符串。要获取类的所有方法的名称,请获取该类对象的所有方法。然后在这些方法对象上调用 getName()。

句法:

public String getName()

返回值:它以字符串形式返回方法的名称。

例子:

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 thid class
    MyThread() {}
 
    // Methods ofthis 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);
 
        // hee notice we change the flaf=g value
        // thai 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");
 
        // Callin the method
        showThreadStatus(thrd);
    }
}


输出: