Quiz: Thread States in Java: The Thread Lifecycle

Total: 6 questions

1. 

What thread states exist in Java, and why are RUNNING and DEAD not among them?

A thread state is a value of the java.lang.Thread.State enum returned by getState(). There are exactly six values:

  • NEW - the Thread object has been created, but start() hasn't been called yet;
  • RUNNABLE - the thread is running or is ready to run and waiting for a CPU;
  • BLOCKED - waiting for a synchronized monitor held by another thread;
  • WAITING - waiting for another thread to act, with no time limit;
  • TIMED_WAITING - the same kind of waiting, but with a timeout;
  • TERMINATED - the run() method has finished.

Running and Dead come from the teaching model in older books (SCJP), not from the API. There's no separate RUNNING because the OS scheduler, not the JVM, controls execution on the CPU: a thread gets and loses the CPU thousands of times a second, so "ready" and "running" are merged into RUNNABLE. Dead is called TERMINATED in the enum.

The result of getState() is a snapshot at the moment of the call: the method is designed for monitoring, not for synchronizing threads.

2. 

How does a Java thread move from one state to another?

The lifecycle starts in NEW (new Thread(...)), goes through RUNNABLE and ends in TERMINATED:

  • start() → RUNNABLE;
  • entering synchronized while the monitor is taken → BLOCKED;
  • Object.wait(), t.join(), LockSupport.park() → WAITING;
  • Thread.sleep(n), wait(n), join(n), parkNanos() → TIMED_WAITING;
  • run() returned normally or threw an uncaught exception → TERMINATED.

From BLOCKED, WAITING and TIMED_WAITING a thread always goes back to RUNNABLE; it never jumps straight to another kind of waiting or to termination. There's no way back to NEW, no way out of TERMINATED, and no direct path from NEW to TERMINATED: a thread whose start() is never called stays NEW forever.

The isAlive() method returns true when the state is neither NEW nor TERMINATED; only getState() can tell those two cases apart.

3. 

What is the difference between the BLOCKED and WAITING states?

BLOCKED - the thread wants to enter a synchronized block or method, but another thread holds the monitor. It moves on by itself as soon as the monitor is free; interrupt() won't pull it out.

WAITING - the thread itself called wait(), join() or park() and is waiting for an event from another thread: notify(), termination, unpark(). interrupt() ends this waiting too - the method throws InterruptedException.

Two interview traps:

  • a thread waiting in ReentrantLock.lock() is WAITING, not BLOCKED: the locks in java.util.concurrent.locks are built on LockSupport.park() (tryLock(timeout) gives TIMED_WAITING). BLOCKED only happens while waiting for a synchronized monitor;
  • a thread woken up by notify() first goes to BLOCKED: it has to reacquire the monitor, and while the notifying thread still holds it, it waits.

Owning a monitor isn't a state: a thread sleeping in sleep() inside synchronized is TIMED_WAITING.

4. 

What state is a thread in while it waits for data in blocking I/O?

A platform thread waiting for data in InputStream.read(), a socket read or System.in.read() is RUNNABLE, not BLOCKED or WAITING. The JVM doesn't track what a thread does inside a system call, so as far as it knows, the thread is "running", even though it uses no CPU.

Thread reader = new Thread(() -> {
    try {
        System.in.read(); // waits for keyboard input
    } catch (IOException e) {
        e.printStackTrace();
    }
});
// 200 ms after start(): reader.getState() == RUNNABLE

So read RUNNABLE as "not waiting for anything the JVM knows about" rather than "working". If a service hangs and every worker thread in the dump is RUNNABLE inside SocketInputStream.read or NioSocketImpl.read, the problem isn't locking but a slow external service or a missing read timeout.

5. 

How can you see the states of all threads in a running JVM?

Take a thread dump: jstack <pid> or jcmd <pid> Thread.print. Each thread in the dump has a java.lang.Thread.State: … line with the same enum value that getState() returns, plus a note in parentheses:

  • TIMED_WAITING (sleeping) - Thread.sleep();
  • BLOCKED (on object monitor) or WAITING (on object monitor) - synchronized or wait();
  • WAITING (parking) - LockSupport.park(), that is, ReentrantLock, queues and the rest of java.util.concurrent.

The - locked <0x...> and - waiting to lock <0x...> lines with the same address show which thread holds a monitor and which one is waiting for it.

6. 

What states can a virtual thread have in Java 21?

The same ones: virtual threads use the Thread.State enum and the getState() method, with no extra values. An unstarted virtual thread is NEW, in Thread.sleep() it's TIMED_WAITING, in join() it's WAITING, and once finished it's TERMINATED.

Thread vt = Thread.ofVirtual().unstarted(task);
System.out.println(vt.isVirtual() + " " + vt.getState()); // true NEW

The noticeable difference is network I/O. A virtual thread waiting for data from a socket is unmounted from its carrier thread and parked, so getState() usually reports WAITING, not RUNNABLE as for a platform thread. "Missing" RUNNABLE threads in read() in a dump after moving to virtual threads are expected behavior.

Page 1 of 1