Thread state in Java is also known as the life cycle of a thread. There are seven life stages for a thread in Java. Any thread used in Java will fall under any of one of those seven categories. The life cycle of a thread is controlled by JVM (Java Virtual Machine).
The following are the life stages of thread and all the thread created in JVM will fall under any one of these categories:
- Time Waiting
At New stage a thread will be created in Java Virtual Machine (JVM) but this thread is yet to be started. While transiting from new to runnable state, the thread will pass through a substate called “ready”. Many programmers count ready as a life cycle stage of thread but actually it is a substate between new and runnable life stages. The thread present in New are considered alive by programmers but JVM consider these threads are yet to be operative.
The thread in Runnable stage is ready for execution. The runnable stage thread may wait for sometime execution because JVM will require some resources from the operating system. During this process the thread scheduler will decide to start, proceed or suspend the particular thread. Thread.yield() is a proposal from JVM to stop the current thread and allow some other thread to execute. The pool which consists of runnable threads are called as runnable pool.
The thread scheduler present in Java Virtual Machine (JVM) will select the thread to transit from runnable stage to running stage. The thread present in running stage will start the execution by entering into run() method. The selection of a particular thread by the thread scheduler is based on the priority. If two thread are scheduled under similar priority, the thread scheduler will choose any one of these threads and it will be followed by the other thread. The thread scheduler’s conduct is highly unpredictable when two threads have similar priority. The use of the method yield() can be used to transform the thread present in running stage to runnable stage.
- More: Java interview Q&A available here
In this thread state, the thread will wait for the specific period of time. The thread is forced into waiting due to calling of one of these methods.
The waiting stage of thread is quite similar to time waiting but the time of waiting period will not be mentioned. The following methods are used to mention a thread in waiting stage:
The thread present in waiting stage usually waits for another thread to complete an intrinsic action. For example: let us consider a thread which called Object.wait to an Object and waiting for an another thread to call Object.notify() on that object. The thread which was called Thread.join will wait for stipulated thread to terminate. The method Suspend() can be used to put a thread in waiting stage and resume() method can be used to transfer the thread to runnable stage.
The thread present in blocked state will wait to enter a coordinated object or method after calling Object.wait(). Mutual exclusion lock by executing thread is acquired by synchronized method. The mutual exclusion lock is owned by executing thread and all the other threads are blocked from acquiring the lock.
If the execution of run() method is complete in a thread. It will be moved into terminate stage. Terminate stage of a thread is also called as dead stage because executions after terminate stages are not allowed. If a thread is executed after its termination, it will throw an exception. The exception thrown after the termination of the thread is called IllegalThreadStateException. The use of the method death(), will directly terminate the thread.