線程類的 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 getStackTrace()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread getThreadGroup()用法及代碼示例
- Java Thread getId()用法及代碼示例
- Java Thread getDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread getName()用法及代碼示例
- Java Thread getPriority()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread destroy()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java Thread sleep()用法及代碼示例
- Java Thread isInterrupted()用法及代碼示例
- Java Thread enumerate()用法及代碼示例
- Java Thread notify()用法及代碼示例
- Java Thread resume()用法及代碼示例
- Java Thread activeCount()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Thread getState() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。