Home

 › 

Articles

 › 

How-to

 › 

Software

 › 

Understanding Life Cycle of Threads in Java, With Examples

Developing programmer Team Development Website design and coding technologies working with life cycle of thread in Java

Understanding Life Cycle of Threads in Java, With Examples

Key Points

  • A thread in Java represents a specific flow of execution within a program, with its own program counter, stack, and local variables.
  • Threads in Java can work together using common data and constructs, with the language offering a wide range of APIs.
  • The life cycle of a thread in Java includes the new, runnable, running, and terminated stages.
  • In the terminated stage, a thread cannot restart, making it crucial to understand termination conditions.

When you’re working with the dynamics of high-level objects, it’s helpful to understand how their execution works. Understanding the life cycle of threads in Java allows you to manage them effectively, including their flow, interaction, and synchronization.

So where do you even begin? In this article, we talk about threads and their patterns. This gives you a base to start on. We even provide a sample syntax to see the life cycle play out in real-time. So let’s get into threads and their mechanisms below.

What Is the Life Cycle of a Thread in Java

Before we get into the life cycle of threads in Java, it’s important to understand what a thread is in the first place. This term represents a specific flow of execution within a program. Each thread has an individual program counter, stack, and local variables that allow it to execute paths on its own.

Threads have the ability to work together using common data and constructs. In Java, these are easy to deal with, as the language uses a wide range of APIs. As such, it’s essential to understand the life cycle of threads in Java as it pertains to each concurrent thread’s execution. Let’s learn more about it below.

How Does the Life Cycle Work?

Each flow of execution within the program works independently, but they all follow a common life cycle. While some threads have unique states or variations, the common pattern goes like this:

  1. New
  2. Runnable
  3. Running
  4. Terminated

In the new stage, we’re creating the thread. It’s now within the code, although it’s not prepared to run.

In the runnable stage, we’ve called the “start()” method. While the thread still hasn’t been executed yet, it’s now eligible. You can call on the thread whenever you’re ready to use it.

In the running stage, the thread is being used.

In the terminated stage, the thread completed its task or received a stop method. When a thread enters the terminated stage, it cannot restart. Therefore, it’s important to understand the termination conditions of your threads so they end when intended.

In the example below, we’ve created a thread that prints out its current state. You can use this code to see how a thread progresses through its life cycle.

public class ThreadLifecycleExample {
    public static void main(String[] args) {
        // Create a new thread
        Thread thread = new Thread(() -> {
            System.out.println("Thread is running");
        });
        
        // Start the thread
        thread.start();
        
        // Get the current state of the thread
        Thread.State state = thread.getState();
        System.out.println("Thread state after start: " + state);
        
        // Wait for the thread to complete execution
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // Get the state of the thread after it has completed
        state = thread.getState();
        System.out.println("Thread state after completion: " + state);
    }
}

In addition to the basic life cycle of thread in Java, other variations exist. For example, “timed waiting” suggests that a thread must wait for a specific duration. Or you might find a thread parked when it’s suspended using low-level synchronization. Other special case states include:

  • Blocked/Waiting
  • Terminated but Not Garbage Collected
  • Interrupted

Summary Table

Thread StageDescription
NewThread is created but not prepared to run
RunnableThread is eligible to run after calling the ‘start()’ method
RunningThread is being used
TerminatedThread completed its task or received a stop method

Frequently Asked Questions

What is a thread in Java?

In Java, a thread is a unit of execution that represents an independent flow of control within a program. It allows for concurrent execution of tasks, enabling parallelism and efficient utilization of system resources.

What is the life cycle of a thread in Java?

The life cycle of a thread in Java consists of the following states: “new” (when the thread is created but not started), “runnable” (when the thread is eligible for execution), “running” (when the thread is executing), “blocked/waiting” (when the thread is waiting for a resource or certain conditions), and “terminated” (when the thread has completed its execution or explicitly terminated).

What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is known for its platform independence, as Java programs can run on different operating systems and devices through the Java Virtual Machine (JVM), and it is widely used for developing various types of applications, including desktop, web, and mobile applications.

What is an API?

An API (Application Programming Interface) is a set of rules and protocols that allows different software applications to communicate with each other. It provides a way for developers to access and utilize the functionality and services provided by a software library, operating system, or platform without needing to understand the internal implementation details.

What special-case states can you find in the life cycle of a thread in Java?

While the basic life cycle includes “new”, “runnable”, “running”, and “terminated”, some threads have action states that are unique to their execution. Some of these states include “timed waiting”, “parked”, “terminated but not garbage collected”, and “interrupted”.

To top