目录

Thread.State getState()

描述 (Description)

java.lang.Thread.getState()方法返回此线程的状态。 它设计用于监视系统状态,而不是用于同步控制。

声明 (Declaration)

以下是java.lang.Thread.getState()方法的声明

public Thread.State getState()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回此线程的状态。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.Thread.getState()方法的用法。

package com.iowiki;
import java.lang.*;
public class ThreadDemo implements Runnable {
   public void run() {
      // returns the state of this thread
      Thread.State state = Thread.currentThread().getState();
      System.out.println(Thread.currentThread().getName());
      System.out.println("state = " + state);
   }
   public static void main(String args[]) {
      Thread t = new Thread(new ThreadDemo());
      // this will call run() function
      t.start();   
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

Thread-0
state = RUNNABLE
↑回到顶部↑
WIKI教程 @2018