Thread类的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
¥ 我要打赏 纠错/补充 收藏
哥,这回真没有了