What are different states of a Thread? What does those states tell us?

By | August 13, 2019

In this blog post we will discuss about one of the very important java interview question  What are different states of a Thread? What does those states tell us?

A thread can have 6 different states as defined in Thread.State enum within thread class. At any given point of time, thread must be in any of these states.

  1. NEW
  2. RUNNABLE
  3. BLOCKED
  4. WAITING
  5. TIMED_WAITING
  6. TERMINATED

NEW

  • A thread is in NEW state when thread just created but not yet started.

RUNNABLE

  • This state is for the currently running thread which is executing in java virtual machine, but it may be waiting for the other resources from operating system such as processor.

BLOCKED                     

  • When a thread said to be BLOCKED state if a thread blocked waiting for a monitor lock. A thread in this state can be waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized method after calling wait()

WAITING

  • A thread is waiting due to calling on one of the method –
    • wait with no timeout
    • join with no timeout
    • park

TIMED_WAITING

When a thread said to be TIMED_WAITING state if a waiting thread with a specified waiting time. A thread is in the TIMED_WAITING state due to calling any one of the following methods with a specified positive waiting time –

  • sleep
  • wait with timeout
  • join with timeout
  • parkNanos
  • parkUnti

TERMINATED

  • Thread state for a terminated thread. The thread has completed execution.

You may also like:

What are common multi-threading issues faced by Java Developers?
What happens when wait() & notify() methods are called?
What is difference between sleep(), yield() and wait() method?

That’s all about  What are different states of a Thread? What does those states tell us?
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published.