Thread.State
is of type enum and represents the states of a thread.
Below
Example shows the different states of a thread in Java
class ThreadState{ public static void main(String args[]){ for(Thread.State c : Thread.State.values()) System.out.println(c); } }
Output
NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED
State
|
Description
|
NEW
|
It is
the state where thread is created and not yet started
|
RUNNABLE
|
A
thread in the runnable state is executing in the Java virtual
machine but it may be waiting for other resources from the
operating system such as processor.
|
BLOCKED
|
It is
the state, where a thread is blocked to acquire a monitor lock
|
WAITING
|
A
thread is in waiting state by calling wait() or join() methods
with no time outs, I.e, with no parameters passing to them
|
TIMED_WAITING
|
A
thread is in the timed waiting state due to calling one of the
methods sleep, wait, join with a specified positive waiting time
|
TERMINATED
|
Thread
completes execution or stopped.
|
Thread.state
has two methods
1.
public static Thread.State[] values()
Returns
the array contains the all enum constants of this type.
2.
public static Thread.State valueOf(String name)
Method
returns the enum constant with the specified String.
Example
class ThreadState{ public static void main(String args[]){ Thread.State const1 = Thread.State.valueOf("NEW"); Thread.State const2 = Thread.State.valueOf("RUNNABLE"); Thread.State const3 = Thread.State.valueOf("TERMINATED"); System.out.println("const1 is " + const1); System.out.println("const2 is " + const2); System.out.println("const3 is " + const3); } }
Output
const1 is NEW const2 is RUNNABLE const3 is TERMINATED
No comments:
Post a Comment