A Java thread is always in one of several states which could be running, sleeping, dead, etc.
Thread States in Java
The different states in which a thread can be are given below –
- New Thread
- Blocked or Waiting State
- Timed – Waiting State
New State – When an instance of a thread is created, a new thread is born and is known to be in the new state. That is, when a thread is created, it directly enters this state.
Further, we can say that when a thread is created but it cannot execute any program is called a new thread state.
Runnable State – When the start method is called on the new instance of the thread, it enters into a runnable state. In a runnable state, the thread is just waiting for execution.
Once, a thread enters the runnable state, it can be run at any instant on the command of the thread scheduler.
Many times there happens, many threads are in the runnable state and the thread scheduler gives the time to threads to run one by one for some time. This happens too fast that the user thinks that all the programs are running concurrently.
Blocked State or Waiting State – When a thread is waiting for some user input of for any other reason, it comes from a runnable state to a non-runnable state. This non-runnable state is only known as a blocked state or waiting state.
Timed Waiting State – When a thread is in the timed waiting state, it is waiting to be going to a runnable state on the completion of the waiting time.
The thread goes into the state when the time-out parameter is executed.
Dead State – When the code of a thread is complete, or any exception is generated, then the thread goes into a 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 in this state.
- Calling any method besides start() or stop() causes an IllegalThreadStateException.
- 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 turn.
Blocked or Waiting State –
This state is also called a not runnable state.
A thread becomes Not Runnable when one of the following events occurs –
- When the 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 waiting for its completion.
Switching from not runnable to runnable –
- If a thread has been put to sleep, then the specified number of milliseconds must elapse.
- If a thread has been suspended, then its resume() method must be invoked
- If a thread is waiting on a condition variable, whatever object owns the variable must relinquish it by calling either notify() of notifyAll().
- If a thread is blocked on I/O, then the I/O must complete.
Timed – Waiting State
- A thread enters the timed waiting state when another thread is currently running and this thread is waiting for its turn to come.
- The turn of the thread to come depends upon the waiting time that is scheduled for the process, or it receives an external notification to do so.
- A thread will enter into a timed waiting state when any of the following functions are called –
- Object#wait() with timeout
- Thread#join() with timeout
- A thread enters this state when the run() method has finished executing or when the stop() method is invoked. Once in this state, the thread cannot ever run again.
In this article, we learned about the different states of threads that are encountered during the life cycle of a thread.
Attempt Free Java MCQ Test