Java教程

Java getState()方法

线程类的 getState()方法返回线程的状态。此方法旨在监视系统状态,而不用于同步控制。

语法

public Thread.State getState()

返回

此方法返回线程的状态。

示例

public class JavaGetStateExp implements Runnable {
    public void run() {
        // returns the state of the thread Thread.State state = Thread.currentThread().getState();
        System.out.println("Running thread name: "+ Thread.currentThread().getName());
        System.out.println("State of thread: " + state);
    }
    public static void main(String args[]) {
        JavaGetStateExp g = new JavaGetStateExp();
        Thread t1= new Thread(g);
        Thread t2= new Thread(g);
        // call run() function t1.start();
        t2.start();
    }
}
输出:
Running thread name: Thread-0State of thread: RUNNABLERunning thread name: Thread-1State of thread: RUNNABLE

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4