-
Table of Contents
The Life Cycle of Thread in Java
Threads are an essential part of Java programming, allowing developers to create concurrent processes that can run simultaneously. Understanding the life cycle of a thread is crucial for writing efficient and scalable multi-threaded applications. In this article, we will explore the various stages of a thread’s life cycle in Java and how they interact with each other.
Thread States
A thread in Java can be in one of several states at any given time. These states include:
- New: When a thread is created but has not yet started.
- Runnable: When a thread is ready to run but is waiting for the CPU.
- Blocked: When a thread is waiting for a resource that is currently held by another thread.
- Waiting: When a thread is waiting indefinitely for another thread to perform a particular action.
- Timed Waiting: When a thread is waiting for a specified amount of time.
- Terminated: When a thread has completed its execution or has been stopped.
Thread Life Cycle
The life cycle of a thread in Java consists of several stages, each with its own characteristics and behaviors. These stages include:
1. New
When a thread is created using the new
keyword, it is in the new state. At this stage, the thread has been instantiated but has not yet started running.
2. Runnable
Once the start()
method is called on a thread object, it transitions to the runnable state. In this state, the thread is ready to run but is waiting for the CPU to execute its code.
3. Blocked
A thread can enter the blocked state when it is waiting for a resource that is currently held by another thread. This can occur when a thread is trying to access a synchronized block of code that is already locked by another thread.
4. Waiting
Threads can enter the waiting state when they are waiting indefinitely for another thread to perform a particular action. This can happen when a thread calls the wait()
method on an object.
5. Timed Waiting
Threads can also enter the timed waiting state when they are waiting for a specified amount of time. This can occur when a thread calls methods such as sleep()
or join()
.
6. Terminated
When a thread has completed its execution or has been stopped using the stop()
method, it enters the terminated state. Once a thread is terminated, it cannot be restarted.
Example
Let’s consider an example to illustrate the life cycle of a thread in Java:
“`java
public class ThreadExample extends Thread {
public void run() {
System.out.println(“Thread is running…”);
}
public static void main(String[] args) {
ThreadExample thread = new ThreadExample();
thread.start();
}
}
“`
In this example, we create a new thread object and start it using the start()
method. The thread transitions from the new state to the runnable state and executes the run()
method, printing “Thread is running…” to the console.
Summary
In conclusion, understanding the life cycle of a thread in Java is essential for writing efficient and scalable multi-threaded applications. By knowing the various states and stages of a thread’s life cycle, developers can create robust and responsive concurrent programs. By following best practices and utilizing Java’s built-in threading mechanisms, developers can harness the power of threads to create high-performance applications.
For more information on Java threading, you can refer to the official Java Concurrency tutorial.