In Java, a thread goes through several states during its life cycle. These states represent the different stages a thread can be in from its creation to its termination.
Here is a detailed explanation of the life cycle of a thread in Java:
1. New
State:NEW
Description: A thread is in the NEW state when it is created but not yet started. At this point, the thread has been instantiated, but the
start() the method has not been called.
Thread thread = new Thread(() -> {
// Task to perform
});
2. Runnable
State:RUNNABLE
Description: A thread enters the RUNNABLE state when the
start() the method is called. In this state, the thread is ready to run and waiting for CPU time. It is also the state where the thread executes its task. However, due to time-sharing and the thread scheduler, a thread in the
RUNNABLE the state may not be actively running.
thread.start(); // Thread transitions to RUNNABLE state
3. Blocked
State:BLOCKED
Description: A thread enters the BLOCKED state when it attempts to access a synchronized block or method that is currently locked by another thread. The thread remains in this state until the lock is released.
synchronized (someObject) {
// Thread may become BLOCKED if another thread holds the lock
}
Waiting
State:WAITING
Description: A thread enters the WAITING state when it is waiting indefinitely for another thread to perform a particular action. This happens when the thread calls methods like
Object.wait() without a timeout, Thread.join() without a timeout, or
LockSupport.park().
synchronized (someObject) {
someObject.wait(); // Thread enters WAITING state
}
Timed Waiting
State:TIMED_WAITING
Description: A thread enters the TIMED_WAITING state when it is waiting for a specified period. This can happen when the thread calls methods like
Thread.sleep(long millis), Object.wait(long timeout),
Thread.join(long millis), or LockSupport.parkNanos(long nanos).
try {
Thread.sleep(1000); // Thread enters TIMED_WAITING state for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
Terminated
State:TERMINATED
Description: A thread enters the TERMINATED state when it has finished executing. This can happen either because the thread's
run() the method has been completed, or because an uncaught exception has terminated the thread.
// After the run() method completes
Thread Life Cycle Diagram
NEW -> RUNNABLE <--> BLOCKED
| | |
V V V
WAITING TIMED_WAITING
|
V
TERMINATED
Summary of Transitions
NEW to RUNNABLE: When start() is called.
RUNNABLE to BLOCKED: When a thread tries to acquire a lock held by another thread.
RUNNABLE to WAITING: When Object.wait(),
Thread.join(), or LockSupport.park() is called.
RUNNABLE to TIMED_WAITING: When Thread.sleep(long),
Object.wait(long), Thread.join(long), or LockSupport.parkNanos(long) is called.
WAITING to RUNNABLE: When Object.notify(),
Object.notifyAll(), or the corresponding event occurs.
TIMED_WAITING to RUNNABLE: When the specified waiting time elapses.
BLOCKED to RUNNABLE: When the thread acquires the lock.
RUNNABLE to TERMINATED: When the thread's run() method completes or an uncaught exception terminates the thread.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Java, a thread goes through several states during its life cycle. These states represent the different stages a thread can be in from its creation to its termination.
Here is a detailed explanation of the life cycle of a thread in Java:
1. New
NEWNEWstate when it is created but not yet started. At this point, the thread has been instantiated, but thestart()the method has not been called.2. Runnable
RUNNABLERUNNABLEstate when thestart()the method is called. In this state, the thread is ready to run and waiting for CPU time. It is also the state where the thread executes its task. However, due to time-sharing and the thread scheduler, a thread in theRUNNABLEthe state may not be actively running.3. Blocked
BLOCKEDBLOCKEDstate when it attempts to access a synchronized block or method that is currently locked by another thread. The thread remains in this state until the lock is released.Waiting
WAITINGWAITINGstate when it is waiting indefinitely for another thread to perform a particular action. This happens when the thread calls methods likeObject.wait()without a timeout,Thread.join()without a timeout, orLockSupport.park().Timed Waiting
TIMED_WAITINGTIMED_WAITINGstate when it is waiting for a specified period. This can happen when the thread calls methods likeThread.sleep(long millis),Object.wait(long timeout),Thread.join(long millis), orLockSupport.parkNanos(long nanos).Terminated
TERMINATEDTERMINATEDstate when it has finished executing. This can happen either because the thread'srun()the method has been completed, or because an uncaught exception has terminated the thread.Thread Life Cycle Diagram
Summary of Transitions
start()is called.Object.wait(),Thread.join(), orLockSupport.park()is called.Thread.sleep(long),Object.wait(long),Thread.join(long), orLockSupport.parkNanos(long)is called.Object.notify(),Object.notifyAll(), or the corresponding event occurs.run()method completes or an uncaught exception terminates the thread.Read more
Explain the difference between checked and unchecked exceptions. Provide examples.
What are annotations in Java? How are they used?
What is the purpose of the volatile keyword in Java?