![]() |
VOOZH | about |
The lifecycle of a thread in Java defines the various states a thread goes through from its creation to termination. Understanding these states helps in managing thread behavior and synchronization in multithreaded applications.
👁 Lifecycle-and-States-of-a-Thread-in-Java
A thread is in the new state when it is created but has not yet started execution, so its code has not begun running.
public static final Thread.State NEW
Thread state for a runnable thread. 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 a processor.
public static final Thread.State RUNNABLE
A thread is in the blocked state when it is waiting to acquire a lock that is currently held by another thread.
public static final Thread.State BLOCKED
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
public static final Thread.State WAITING
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
public static final Thread.State TIMED_WAITING
Thread state for a terminated thread. The thread has completed execution.
public static final Thread.State TERMINATED
Example: Demonstrate thread states using a ticket booking scenario
Output:
Explanation: