Master Java Thread Lifecycle: From NEW to TERMINATED Explained
Understanding Java's thread lifecycle—from creation (NEW) through runnable execution, waiting states, blocking, and termination—is essential for multithreaded development, and this guide breaks down each state, transitions, and relevant code examples to help you master concurrency concepts.
Java thread lifecycle is crucial for multithreaded development and a common interview question.
Thread Lifecycle Overview
Diagram shows all states.
States: NEW, RUNNABLE, WAITING, TIMED_WAITING, BLOCKED, TERMINATED.
NEW – initial state, thread created but not started.
RUNNABLE – thread ready to run or executing.
WAITING – waiting indefinitely for another thread.
TIMED_WAITING – waiting with a timeout.
BLOCKED – waiting to acquire a lock.
TERMINATED – thread has finished execution.
These states can be grouped into normal flow and abnormal situations.
Normal flow: NEW → RUNNABLE → TERMINATED.
Abnormal situations: thread may enter WAITING, TIMED_WAITING, or BLOCKED before returning to RUNNABLE.
NEW
A thread is in NEW state after creation and before start() is called.
Example:
Thread t = new MyThread();RUNNABLE
After calling start(), the thread moves from NEW to RUNNABLE.
In RUNNABLE, the thread may be executing or waiting for CPU time allocated by the JVM scheduler.
Example:
Thread t = new MyThread();
t.start();WAITING
A thread enters WAITING when it calls methods such as Object.wait(), Thread.join(), or LockSupport.park() without a timeout.
Example: threadA calls threadB.join() and waits until threadB finishes.
TIMED_WAITING
Similar to WAITING but with a time limit, entered via methods like Thread.sleep(), Object.wait(long), Thread.join(long), LockSupport.parkNanos(), or LockSupport.parkUntil().
BLOCKED
A thread is BLOCKED when it attempts to enter a synchronized block or method that is already held by another thread.
Example: threadA holds synchronized method A(), threadB trying to call A() becomes BLOCKED.
TERMINATED
The thread has completed execution, either normally or due to an exception.
Summary
The lifecycle diagram shows the normal path NEW → RUNNABLE → TERMINATED and the possible abnormal paths involving WAITING, TIMED_WAITING, and BLOCKED, after which the thread returns to RUNNABLE before termination.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
