Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference
In Java, the lifecycle of a thread encompasses several states that a thread goes through from its creation to its termination. Understanding these states is crucial for effective multithreaded programming. Here are the key states of a thread’s lifecycle in Java:
  1. New: A thread is in the “New” state when it is created but not yet started. It remains in this state until the start() method is called on the Thread object.
				
					Thread t = new Thread(() -> {
    // thread code
});

				
			

2. Runnable: After the start() method is called, the thread enters the “Runnable” state. It is ready to run and is waiting for the CPU to allocate time for it. Note that a thread in this state is not guaranteed to be running; it simply means it is ready to be picked up by the thread scheduler.

				
					t.start();  // Moves thread to Runnable state

				
			

3. Running: A thread is in the “Running” state when the thread scheduler picks it from the Runnable pool and the thread is actively executing its task.

				
					public void run() {
    // thread is running
}

				
			

4. Blocked/Waiting: A thread enters the “Blocked” or “Waiting” state when it is waiting for a monitor lock to enter a synchronized block/method or waiting for another thread to perform a particular action.

  • Blocked: Waiting to acquire a lock.
				
					synchronized(lock) {
    // thread waits here if lock is not available
}

				
			
  • Waiting: Indefinitely waiting for another thread to perform a particular action.
				
					synchronized (obj) {
    obj.wait();  // thread waits indefinitely
}

				
			
  • Timed Waiting: Waiting for another thread to perform a specific action up to a specified waiting time.
				
					synchronized (obj) {
    obj.wait(1000);  // thread waits up to 1000 milliseconds
}

				
			

5. Terminated: A thread is in the “Terminated” state once its run method has completed. The thread has finished execution either by returning from the run() method or by throwing an exception that was not caught.

				
					@Override
public void run() {
    // task
    // after completing the task, thread terminates
}

				
			
  • Here’s an example demonstrating the lifecycle of a thread:
				
					public class ThreadLifecycleExample {

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Thread is running");
            try {
                Thread.sleep(2000); // Simulate some work with sleep
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread is terminating");
        });

        System.out.println("Thread state after creation: " + t.getState()); // NEW

        t.start();
        System.out.println("Thread state after calling start(): " + t.getState()); // RUNNABLE

        try {
            Thread.sleep(500); // Main thread sleeps to allow time for the new thread to start
            System.out.println("Thread state during sleep: " + t.getState()); // TIMED_WAITING or RUNNABLE
            t.join(); // Wait for the new thread to terminate
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Thread state after termination: " + t.getState()); // TERMINATED
    }
}

				
			

Key Points:

  • New: When a thread is instantiated.
  • Runnable: When the start() method is called.
  • Running: When the thread scheduler picks it for execution.
  • Blocked/Waiting/Timed Waiting: When it is waiting for a lock or another thread’s action.
  • Terminated: When the run() method completes or an uncaught exception occurs.

Understanding these states and transitions is essential for managing thread behavior and synchronization in concurrent applications.

Scroll to Top