Showing posts with label Java Threads. Show all posts
Showing posts with label Java Threads. Show all posts

Tuesday, 29 November 2016

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

🚀 Want to Master Java Concurrency?

Subscribe to Ram N Java for beginner-friendly, high-quality programming guides!

🔔 Subscribe Now

What is a Thread Life Cycle in Java?

In Java, multithreading allows concurrent execution of two or more parts of a program to maximize CPU utilization. From creation to termination, every thread transitions through several distinct states managed by the JVM and OS scheduler.

Key States in Java Thread Life Cycle

  • New (Born): When a thread instance is created using the Thread class or Runnable interface, before calling start().
  • Runnable: After invoking start(), the thread becomes ready to run and waits for CPU time allocation from the thread scheduler.
  • Blocked / Waiting: When a thread is waiting to acquire a monitor lock or waiting indefinitely for another thread's notification.
  • Timed Waiting: When a thread waits for a specified time period (e.g., using sleep(ms) or join(ms)).
  • Terminated (Dead): When the thread finishes executing its run() method or terminates abruptly due to an unhandled exception.

Why Master Thread States?

Understanding these transitions helps you write robust concurrent applications, avoid common concurrency pitfalls like deadlocks and race conditions, and optimize system performance effectively.

Related Tutorials from Ram N Java


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

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

    🚀 Want to Master Java Concurrency?

    Subscribe to Ram N Java for simplified, beginner-friendly coding tutorials!

    🔔 Subscribe Now

    How Threads Work in Java

    A thread is a lightweight sub-process that allows a program to operate more efficiently by doing multiple tasks simultaneously. When you execute a Java program, the Java Virtual Machine (JVM) allocates execution paths, enabling concurrent processing through the thread life cycle.

    Step-by-Step Thread Execution Flow

    • 1. Instantiation: You create a thread object either by extending the Thread class or implementing the Runnable interface.
    • 2. Invoking start(): Calling start() notifies the thread scheduler that the thread is ready to execute its run() method.
    • 3. Execution & Scheduling: The JVM Thread Scheduler picks the thread from the ready pool and allocates CPU time slices to execute the business logic.
    • 4. Pausing & Resuming: Threads can temporarily enter waiting, timed-waiting, or blocked states during I/O operations or lock acquisition.
    • 5. Termination: Once the run() method finishes execution, the thread completes its life cycle and is garbage collected.

    Key Takeaway

    Understanding how threads transition between different states ensures you can build responsive, high-performance Java applications without common pitfalls like race conditions and resource leaks.

    Related Tutorials from Ram N Java


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

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