Tuesday, 29 November 2016

Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V1)

🚀 Master Java & Boost Your Coding Skills!

Subscribe to Ram N Java for crystal-clear programming tutorials and step-by-step guides.

🔔 Subscribe Now

Introduction to Thread Life Cycle in Java

In Java multithreading, a thread always exists in any one of several defined states. From the moment it is created until it finishes execution, the life cycle of a thread is strictly controlled and managed by the Java Virtual Machine (JVM) and the thread scheduler.

The 5 Core States of a Java Thread

1. New State

A thread is in the New state when you create an instance of the Thread class (or subclass), but before the start() method is called. At this stage, the thread is not yet alive in the scheduling queue.

2. Runnable State

Once the start() method is invoked, the thread transitions to the Runnable state. It is ready for execution and waiting for the thread scheduler to pick it up.

3. Running State

When the thread scheduler selects the thread from the runnable pool, it enters the Running state and begins executing the code inside its run() method.

4. Blocked / Non-Runnable State

A thread is still alive but temporarily inactive and not eligible to run. It enters this state due to:

  • Calling Thread.sleep()
  • Waiting for I/O operations to finish
  • Waiting for a lock / resource (synchronization block)
  • Calling wait() or suspend()

Once the sleep time expires, I/O completes, the lock becomes available, or a notification is received (notify()/notifyAll()), the thread transitions back to the Runnable state.

5. Terminated (Dead) State

A thread enters the Terminated state when its run() method finishes execution or if an unhandled exception terminates it. Once terminated, the thread cannot be restarted.

Summary of State Transitions

New → Runnable → Running → Blocked/Waiting → Runnable → Running → Terminated

Recommended Related Tutorials


Click here to watch in Youtube :
https://www.youtube.com/watch?v=i9vMYLRUjfE&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V1) 
See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Tutorials