The enum Thread.State class and the new getState () API are designed to request thread execution status.
A thread can only be in one state at a time. These states are virtual machine states that do not reflect the state of the operating system threads [ NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED ].
enum Thread.State extends Enum implements Serializable , Comparable
getState () jdk5 - public State getState() {...} " Returns the state of this stream. This method is intended for use in monitoring the state of the system, and not for controlling synchronization.
isAlive () - public final native boolean isAlive(); " Returns true if the thread for which it is called is still alive, otherwise it returns false . The thread is alive if it was started and has not yet died.
Sample source code for java.lang.Thread and sun.misc.VM
package java.lang; public class Thread implements Runnable { public final native boolean isAlive();
Example with java.util.concurrent.CountDownLatch for parallel execution of several threads. After all threads have completed, the main thread is executed. (until the parallel threads complete their task, the main thread will be blocked.)
public class MainThread_Wait_TillWorkerThreadsComplete { public static void main(String[] args) throws InterruptedException { System.out.println("Main Thread Started...");
@See also
- implements runnable versus extends thread
- Stream states
- Flow state diagram
- How to start two threads at the same time
Yash Jan 09 '18 at 11:09 2018-01-09 11:09
source share