What are the six states of a Java thread, and how does a thread move between them?
A Java thread's lifecycle passes through six distinct states. NEW means the thread object has been created but start() has not yet been called. RUNNABLE covers both a thread that is actively executing on the CPU and one that is ready to run and waiting for CPU time. BLOCKED means the thread is waiting to acquire a synchronized lock that another thread currently holds. WAITING means the thread is waiting indefinitely for another thread to act, typically via wait(), join(), or LockSupport.park(). TIMED_WAITING is the same idea but with a timeout, as with sleep(n), wait(n), or join(n). TERMINATED means the run() method has completed or the thread has thrown an uncaught exception. You can inspect a thread's current state at any time by calling thread.getState(); a thread can never move back to NEW once it has been started, and TERMINATED is a final state with no further transitions.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response