当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Thread 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-0
State of thread:RUNNABLE
Running thread name:Thread-1
State of thread:RUNNABLE






相关用法


注:本文由纯净天空筛选整理自 Java Thread getState() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。