getState()包java.lang.Thread.getState()中提供了此方法。
此方法用于返回此线程的状态。
当我们执行一个线程时,有多种状态可以正常执行该线程[线程的状态,如启动,就绪,运行,等待,阻塞,终止]。
此方法不是最终方法,因此我们可以在子类中重写此方法。
此方法的返回类型为Thread.State,因此它返回此线程的状态。
此方法不会引发任何异常。
语法:
Thread.State getState(){
}参数:
在Thread方法中,我们不传递任何对象作为参数。
返回值:
该方法的返回类型为Thread.State,它返回此线程的状态。
getState()方法示例/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class GetThreadState extends Thread {
//覆盖run()Thread类
public void run() {
//通过使用getState()方法用于返回
//该线程的状态
System.out.println("The state of this thread is : " + Thread.currentThread().getState());
/* This is another way of writing the above statement
Thread.State th_state = Thread.currentThread().getState();
System.out.println("The state of this thread is : "+th_state);*/
}
public static void main(String[] args) {
//创建一个GetThreadState类的对象
GetThreadState gt_state = new GetThreadState();
//我们正在设置线程的名称GetThreadState-
gt_state.setName("GetThreadState");
//start()使用GetThreadState类的调用方法
//线程类的对象/
gt_state.start();
//通过使用getName()方法返回此名称
//线程[GetThreadState]
System.out.println("The name of this thread is " + " " + gt_state.getName());
}
}输出结果
E:\Programs>javac GetThreadState.java E:\Programs>java GetThreadState The name of this thread is GetThreadState The state of this thread is : RUNNABLE