What State is a Thread in When it is Executing?

An executing thread is in the running state.

When a thread in Java is executing, it is in the “Runnable” state. In the multithreading context of Java, a thread goes through different states in its lifecycle. The main states are:

  1. New: The thread is in this state before the start() method is called on it.
  2. Runnable: The thread is ready to run and is waiting for the CPU to execute it. When the thread scheduler picks it, it moves to the running state.
  3. Blocked: The thread is waiting for a monitor lock to enter a synchronized block/method or waiting for some I/O operation to complete.
  4. Waiting: The thread is in this state due to a call to the wait() method, and it is waiting for another thread to notify or notifyAll() to wake it up.
  5. Timed Waiting: Similar to the waiting state, but with a specified waiting time. Threads enter this state due to methods like sleep() or join() with a timeout.
  6. Terminated: The thread has completed its execution or terminated due to an uncaught exception.

So, when a thread is actively executing its code, it is in the “Runnable” state.