this.getstate
線程類Thread.State getState() (Thread Class Thread.State getState())
This method is available in package java.lang.Thread.getState().
軟件包java.lang.Thread.getState()中提供了此方法。
This method is used to return the state of this thread.
此方法用于返回此線程的狀態。
When we execute a thread so there are various states for normal execution of the thread [States of the Thread like, start, ready, running, waiting, blocked, terminate].
當我們執行一個線程時,有多種狀態可以正常執行該線程[線程的狀態,如啟動,就緒,運行,等待,阻塞,終止]。
This method is not final so we can override this method in child class.
此方法不是最終方法,因此我們可以在子類中重寫此方法。
The return type of this method is Thread.State so it returns the state of this thread
此方法的返回類型為Thread.State,因此它返回此線程的狀態。
This method does not raise any exception.
此方法不會引發任何異常。
Syntax:
句法:
Thread.State getState(){
}
Parameter(s):
參數:
We don't pass any object as a parameter in the method of the Thread.
我們不會在Thread方法中將任何對象作為參數傳遞。
Return value:
返回值:
The return type of this method is Thread.State, it returns the state of this thread.
該方法的返回類型為Thread.State ,它返回此線程的狀態。
Java程序演示getState()方法的示例 (Java program to demonstrate example of getState() method)
/* 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 {
// Override run() of Thread class
public void run() {
// By using getState() method is used to return
// the state of this thread
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) {
// Creating an object of GetThreadState class
GetThreadState gt_state = new GetThreadState();
// We are setting the name of the thread GetThreadState
gt_state.setName("GetThreadState");
// Calling start() method with GetThreadState class
// object of Thread class/
gt_state.start();
// By using getName() method to return the name of this
// thread [GetThreadState]
System.out.println("The name of this thread is " + " " + gt_state.getName());
}
}
Output
輸出量
E:\Programs>javac GetThreadState.java
E:\Programs>java GetThreadState
The name of this thread is GetThreadState
The state of this thread is : RUNNABLE
翻譯自: https://www.includehelp.com/java/thread-class-thread-state-getstate-method-with-example.aspx
this.getstate