Java Examples - Getting Thread State
In the Java thread lifecycle, the Thread class has an enum type State that defines several thread states:
- New
- Runnable
- Blocked
- Waiting
- Timed Waiting
- Terminated
Description of each state:
1. Initial state - NEW
Declaration:
public static final Thread.State NEW
Implementing the Runnable interface and extending Thread both produce a thread class; once an instance is created with new, the thread enters the initial state.
2. RUNNABLE
Declaration:
public static final Thread.State RUNNABLE
2.1. Runnable state
The runnable state only means you are eligible to run; if the scheduler has not picked you, you remain in the runnable state forever.
Calling a thread's start() method puts that thread into the runnable state.
When the current thread's sleep() method finishes, another thread's join() finishes, the wait for user input completes, or a thread acquires an object lock, those threads also enter the runnable state.
When the current thread's time slice is used up or the current thread's yield() method is called, the current thread enters the runnable state.
A thread in the lock pool enters the runnable state after acquiring the object lock.
2.2. Running state
The state a thread is in when the thread scheduler selects it from the runnable pool as the current thread. This is also the only way for a thread to enter the running state.
3. Blocked state - BLOCKED
Declaration:
public static final Thread.State BLOCKED
The blocked state is the state of a thread blocked while entering a method or code block modified by the synchronized keyword (acquiring the lock).
4. Waiting - WAITING
Declaration:
public static final Thread.State WAITING
Threads in this state are not allocated CPU execution time; they must wait to be explicitly woken up, otherwise they remain in an indefinite waiting state.
5. Timed waiting - TIMED_WAITING
Declaration:
public static final Thread.State TIMED_WAITING
Threads in this state are not allocated CPU execution time, but they do not need to wait indefinitely to be explicitly woken up by other threads; after a certain time they wake up automatically.
6. Terminated state - TERMINATED
Declaration:
public static final Thread.State TERMINATED
When a thread's run() method completes, or the main thread's main() method completes, we consider it terminated. The thread object may still be alive, but it is no longer a separately executing thread. Once a thread is terminated, it cannot be revived.
Calling the start() method on a terminated thread throws a java.lang.IllegalThreadStateException.
The following example demonstrates how to get the state of a thread:
Main.java File
//
Java program - demonstrating thread states
class
thread
implements
Runnable
{
public
void
run
(
)
{
//
thread2 - timed waiting
try
{
Thread
.
sleep
(
1500
)
;
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
(
)
;
}
System
.
out
.
println
(
"
State of thread1 while it called join() method on thread2 -
"
Test
.
thread1
.
getState
(
)
)
;
try
{
Thread
.
sleep
(
200
)
;
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
public
class
Test
implements
Runnable
{
public
static
Thread
thread1
;
public
static
Test
obj
;
public
static
void
main
(
String
[
]
args
)
{
obj
=
new
Test
(
)
;
thread1
=
new
Thread
(
obj
)
;
//
Create thread1, now in the initial state
System
.
out
.
println
(
"
State of thread1 after creating it -
"
thread1
.
getState
(
)
)
;
thread1
.
start
(
)
;
//
thread1 - runnable state
System
.
out
.
println
(
"
State of thread1 after calling .start() method on it -
"
thread1
.
getState
(
)
)
;
}
public
void
run
(
)
{
thread
myThread
=
new
thread
(
)
;
Thread
thread2
=
new
Thread
(
myThread
)
;
//
Create thread1, now in the initial state
System
.
out
.
println
(
"
State of thread2 after creating it -
"
thread2
.
getState
(
)
)
;
thread2
.
start
(
)
;
//
thread2 - runnable state
System
.
out
.
println
(
"
State of thread2 after calling .start() method on it -
"
thread2
.
getState
(
)
)
;
//
moving thread1 to timed waiting state
try
{
//
moving - timed waiting
Thread
.
sleep
(
200
)
;
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
(
)
;
}
System
.
out
.
println
(
"
State of thread2 after calling .sleep() method on it -
"
thread2
.
getState
(
)
)
;
try
{
//
Wait for thread2 to terminate
thread2
.
join
(
)
;
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
(
)
;
}
System
.
out
.
println
(
"
State of thread2 when it has finished it's execution -
"
thread2
.
getState
(
)
)
;
}
}
The output of the above code is:
State of thread1 after creating it - NEW
State of thread1 after calling .start() method on it - RUNNABLE
State of thread2 after creating it - NEW
State of thread2 after calling .start() method on it - RUNNABLE
State of thread2 after calling .sleep() method on it - TIMED_WAITING
State of thread1 while it called join() method on thread2 -WAITING
State of thread2 when it has finished it's execution - TERMINATED
Java Examples