Tuesday, 29 November 2016

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

Enjoying Java Tutorials?

Subscribe to our YouTube channel for clear, beginner-friendly core Java and multi-threading tutorials!

👉 Click Here to Subscribe

Introduction to Java Thread Life Cycle

In Java, multithreading allows concurrent execution of two or more parts of a program. During its execution, a thread undergoes various transitions from creation to termination. Understanding these life cycle states is essential for building efficient, high-performance Java applications.

Key States in a Thread's Life Cycle

1. New (Born) State

When an instance of the Thread class is created using the new operator, the thread is in the New state. At this stage, it is not yet ready to run and has not been allocated system resources.

2. Runnable State

Invoking the start() method moves the thread into the Runnable state. This does not immediately run the thread, but makes it eligible for execution by the Thread Scheduler.

3. Running State

When the Thread Scheduler selects the runnable thread and allocates CPU time, it moves into the Running state and begins executing code inside its run() method.

4. Blocked / Waiting / Sleeping State

A running thread can transition out of execution temporarily due to various reasons:

  • Sleeping: Calling Thread.sleep() puts the thread to sleep for a specified duration before returning to the runnable state.
  • Waiting: Calling wait() pauses the thread until another thread invokes notify() or notifyAll().
  • Blocked: The thread is suspended while waiting for an I/O operation or monitor lock.

5. Terminated (Dead) State

Once the run() method finishes execution or terminates due to an unhandled exception, the thread reaches the Terminated state and cannot be restarted.

Related Video Tutorials


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

Click the below Image to Enlarge
Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V4) 
Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V4) 
Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V4) 
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