During the java thread life cycle, there are many states it can enter. They include:
A thread is always in one of these five states. It can move from one state to another via a variety of ways. Shown in below figure.
When we create a thread object, the thread is born and is said to be in newborn state. The thread is not yet scheduled for running. At this state, we can do only one of the following things with it:
The runnable state means that the thread is ready for execution and is waiting for the availability of the processor. That is the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority, then they are given time slots for execution in round robin fashion, i.e. first-come, first-serve manner. The thread that relinquishes control joins the queue at the end and again waits for its turn. This process of assigning time to threads is known as time-slicing.
However, if we cant a thread to relinquish control to another thread of equal priority before its turn comes, we can do so by using the yield() method.
Running means that the processor has given its time to the thread for its execution. The thread runs until it relinquish its control in one of the following situations.
A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. this happens when the thread is suspended, sleeping, or waiting in order to satisfy certain requirements. A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.
Every thread has a life cycle. A running thread ends its life when it has completed executing its run() method. It is a natural death. However, we can kill it by sending the stop message to it at any state thus causing a premature death to it. A thread can be killed as soon it is born, or while it is running, or even when it is in “not runnable” (blocked) condition.