Java Thread Lifecycle
Quick Answer
Thread Lifecycle explains understanding the lifecycle of a thread is essential for writing efficient and safe multithreaded Java applications.
Learning Objectives
- Explain the purpose of Thread Lifecycle in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Thread Lifecycle.
- Apply Thread Lifecycle in a simple real-world scenario or practice task.
Introduction
Understanding the lifecycle of a thread is essential for writing efficient and safe multithreaded Java applications.
This tutorial explains the different states a thread goes through, how transitions happen, and how to manage threads properly.
Multithreading is not about doing more things at once, but about doing things efficiently.
What is a Thread in Java?
A thread is a lightweight subprocess, the smallest unit of processing that can be scheduled by the Java Virtual Machine (JVM).
Java supports multithreading, allowing multiple threads to run concurrently within a single program.
- Threads share the same memory space but have their own execution stack.
- They enable parallel execution of code, improving performance on multi-core processors.
Thread Lifecycle States
A Java thread goes through several states from creation to termination.
Understanding these states helps in controlling thread behavior and avoiding concurrency issues.
- New
- Runnable
- Running
- Blocked
- Waiting
- Timed Waiting
- Terminated
| State | Description |
|---|---|
| New | Thread is created but not yet started. |
| Runnable | Thread is ready to run and waiting for CPU time. |
| Running | Thread is executing its task. |
| Blocked | Thread is waiting to acquire a monitor lock. |
| Waiting | Thread is waiting indefinitely for another thread's action. |
| Timed Waiting | Thread is waiting for a specified amount of time. |
| Terminated |
Detailed Explanation of Thread States
Let's explore each thread state in detail to understand their roles and transitions.
New State
When a thread object is instantiated using the Thread class or Runnable interface, it is in the New state.
At this point, the thread has not started executing.
- Thread created with new Thread()
- No system resources allocated yet
Runnable State
Calling the start() method on a thread moves it to the Runnable state.
The thread is ready to run and waiting for the JVM thread scheduler to allocate CPU time.
- Thread is eligible to run but may not be running immediately
- Scheduler decides when the thread runs
Running State
When the thread scheduler picks a thread from the Runnable pool, it moves to the Running state.
The thread executes its run() method during this state.
- Thread actively executes code
- Can move back to Runnable if preempted
Blocked State
Thread Lifecycle Transitions
Threads transition between states based on method calls and scheduler decisions.
Understanding these transitions is key to managing thread behavior.
- New -> Runnable: start() method called
- Runnable -> Running: Scheduler picks thread
- Running -> Blocked: Waiting for monitor lock
- Running -> Waiting: wait(), join() without timeout
- Running -> Timed Waiting: sleep(), wait() with timeout
- Waiting/Timed Waiting -> Runnable: notified or timeout
- Running -> Terminated: run() completes or thread stops
Example: Demonstrating Thread Lifecycle
The following example shows a simple thread lifecycle with print statements indicating state changes.
Practical Example
This example creates a thread, starts it, and prints its state at different points to illustrate the lifecycle transitions.
Examples
public class ThreadLifecycleDemo {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("Thread is running");
try {
Thread.sleep(1000); // Timed Waiting
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread finished execution");
});
System.out.println("Thread state after creation: " + thread.getState()); // NEW
thread.start();
System.out.println("Thread state after start(): " + thread.getState()); // RUNNABLE
try {
Thread.sleep(500);
System.out.println("Thread state during sleep: " + thread.getState()); // TIMED_WAITING
thread.join();
System.out.println("Thread state after completion: " + thread.getState()); // TERMINATED
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}This example creates a thread, starts it, and prints its state at different points to illustrate the lifecycle transitions.
Best Practices
- Always start threads using the start() method, not run().
- Avoid holding locks for long periods to prevent blocking other threads.
- Use wait() and notify() carefully to coordinate thread communication.
- Handle InterruptedException properly to respond to thread interruptions.
- Prefer higher-level concurrency utilities from java.util.concurrent package for complex scenarios.
Common Mistakes
- Calling run() directly instead of start(), which does not create a new thread.
- Ignoring thread state checks leading to illegal thread operations.
- Not synchronizing shared resources causing race conditions.
- Failing to handle InterruptedException, causing unexpected thread behavior.
- Trying to restart a terminated thread, which is not allowed.
Hands-on Exercise
Thread State Transitions
Write a Java program that creates a thread and prints its state at each lifecycle stage.
Expected output: Console output showing thread states: NEW, RUNNABLE, TIMED_WAITING, TERMINATED.
Hint: Use Thread.getState() method and appropriate sleep or wait calls to observe states.
Interview Questions
What are the different states of a Java thread?
InterviewThe states are New, Runnable, Running, Blocked, Waiting, Timed Waiting, and Terminated.
How do you move a thread from New to Runnable state?
InterviewBy calling the start() method on the thread object.
Can a terminated thread be restarted?
InterviewNo, once a thread is terminated, it cannot be restarted.
MCQ Quiz
1. What is the best first step when learning Thread Lifecycle?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Thread Lifecycle?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. Understanding the lifecycle of a thread is essential for writing efficient and safe multithreaded Java applications.
B. Thread Lifecycle never needs examples
C. Thread Lifecycle is unrelated to practical work
D. Thread Lifecycle should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Understanding the lifecycle of a thread is essential for writing efficient and safe multithreaded Java applications.
- This tutorial explains the different states a thread goes through, how transitions happen, and how to manage threads properly.
- A thread is a lightweight subprocess, the smallest unit of processing that can be scheduled by the Java Virtual Machine (JVM).
- Java supports multithreading, allowing multiple threads to run concurrently within a single program.
- A Java thread goes through several states from creation to termination.
Summary
The Java thread lifecycle consists of several states that represent the thread's status during execution.
Understanding these states and their transitions helps in writing robust multithreaded applications.
Proper management of thread states prevents common concurrency problems and improves application performance.
Frequently Asked Questions
What method starts a Java thread?
The start() method is used to start a Java thread and move it from New to Runnable state.
What is the difference between Runnable and Running states?
Runnable means the thread is ready to run and waiting for CPU time, while Running means the thread is actively executing.
How can a thread enter the Waiting state?
A thread enters Waiting state when it calls wait() or join() without a timeout and waits indefinitely.
What is Thread Lifecycle?
Understanding the lifecycle of a thread is essential for writing efficient and safe multithreaded Java applications.
Why is Thread Lifecycle important?
This tutorial explains the different states a thread goes through, how transitions happen, and how to manage threads properly.
How should I practice Thread Lifecycle?
A thread is a lightweight subprocess, the smallest unit of processing that can be scheduled by the Java Virtual Machine (JVM).

