java getstate_Java线程类Thread.State getState()方法(带示例)

线程类Thread.State getState()包java.lang.Thread.getState()中提供了此方法。

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

当我们执行一个线程时,有多种状态可以正常执行该线程[线程的状态,如启动,就绪,运行,等待,阻塞,终止]。

此方法不是最终方法,因此我们可以在子类中重写此方法。

此方法的返回类型为Thread.State,因此它返回此线程的状态。

此方法不会引发任何异常。

语法:Thread.State getState(){

}

参数:

在Thread方法中,我们不传递任何对象作为参数。

返回值:

该方法的返回类型为Thread.State,它返回此线程的状态。

Java程序演示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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值