Life cycle of the thread is valuable in the large multithreading application. State of a thread specifies the current activities of the thread at any movement. There are four basic state of the thread.
1. New – when the object of the class that representing a thread after creating the object, is created then before call the start method the thread can’t run. So it is called new state of the thread or in other words we can say that the thread is said to be in new state. It means the thread has been created and it is ready to run.
2. Runnable --Thread exists in several states. A thread that has been just created is in the new state. The thread remains in this state until the thread’s start method is called. This causes the thread to enter the runnable (ready) state when the system assigns a processor to the thread
- Running – A thread is said to be in the running state if it is executing instructions. The instructions are placed in the run method and when the run method is executing, and then the thread is in the running state. The run method is called by the start() method automatically.
- Dead- when the thread returns from run method, it is said to be in the dead state. After the return from the run() the thread instance will available for latter use .
- Blocked – the thread could be run but there is something that prevents it. While in the blocked state the scheduler will simply skip over it and not give it any CPU time. Until a thread re-entered the running state it will not perform any operations.
Checking the thread state
New release 5.0 of java provides the facility to check the state of the thread at any instance. When the method getState() is called on a thread, one of the following
Thread.State
values is returned: ·
NEW
·
RUNNABLE
·
BLOCKED
·
WAITING
·
TIMED_WAITING
·
TERMINATED
Comments