Tuesday, 29 November 2016

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

Want to Master Core Java?

Subscribe to our channel for simplified tutorials on Java Multithreading and Core Concepts!

👉 Subscribe for More Tutorials

Understanding the Life Cycle of a Thread in Java

Every thread in Java follows a specific path from the moment it is initialized until it completes its execution and gets destroyed. Understanding these distinct states helps you manage concurrency effectively and avoid issues like deadlocks and race conditions.

Detailed Stages: From Creation to Destruction

1. Creation (New State)

A thread begins its life cycle in the New state when an instance of the Thread class is instantiated. At this stage, code execution has not started, and the thread is simply an object residing in memory.

2. Runnable and Running

When you call the start() method, the thread moves to the Runnable state. Once the operating system's thread scheduler allocates CPU execution time, the thread transitions into the Running state, executing the logic written inside the run() method.

3. Non-Runnable (Blocked / Waiting / Timed Waiting)

While executing, a thread can temporarily pause its execution due to several conditions:

  • Blocked: Waiting to acquire a monitor lock held by another thread.
  • Waiting: Suspended until another thread issues a notification using notify() or notifyAll().
  • Timed Waiting: Paused for a specified duration using methods such as Thread.sleep(milliseconds).

4. Destruction (Dead / Terminated State)

A thread reaches the Terminated (dead) state once its run() method execution completes naturally or if an uncaught exception halts it. A thread in this state cannot be re-started.

Related Video Tutorials


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

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