A Java thread is always in one of several states which could be running, sleeping, dead, etc.
A thread can be in any of the following states:
- New Thread state (Ready-to-run state)
- Runnable state (Running state)
- Not Runnable state
- Dead state
A thread is in this state when the instantiation of a Thread object creates a new thread but does not
start it running. A thread starts life in the Ready-to-run state. You can call only the start() or stop()
methods when the thread is in this state. Calling any method besides start() or stop() causes an
When the start() method is invoked on a New Thread() it gets to the runnable state or running state by
calling the run() method. A Runnable thread may actually be running, or may be awaiting its turn to run.
A thread becomes Not Runnable when one of the following four events occurs:
- When sleep() method is invoked and it sleeps for a specified amount of time
- When suspend() method is invoked
- When the wait() method is invoked and the thread waits for notification of a free resource or waits for
the completion of another thread or waits to acquire a lock of an object.
- The thread is blocking on I/O and waits for its completion
Note: Thread.currentThread() may return an output like Thread[threadA,5,main]
The output shown in bold describes
- the name of the thread,
- the priority of the thread, and
- the name of the group to which it belongs.
Here, the run() method put itself to sleep for one second and becomes Not Runnable during that period.
A thread can be awakened abruptly by invoking the interrupt() method on the sleeping thread object or at the end of the period of time for sleep is over. Whether or not it will actually start running depends on its priority and the availability of the CPU.
Hence I hereby list the scenarios below to describe how a thread switches form a non runnable to a runnable state: