Wednesday, 30 November 2016

Java Tutorial : Java Threads (How to create a thread in java) - Playlist

Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java)


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

Click the below Image to Enlarge
Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java) 
Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java) 
 DisplayRunnable.java
/*
 * If you are not extending the Thread class,your class
 * object would not be treated as a thread object.So you
 * need to explicitly create Thread class object.We are
 * passing the object of your class that implements
 * Runnable so that your class run() method may execute.
 */
public class DisplayRunnable implements Runnable
{
    public static void main(String args[])
    {
        DisplayRunnable displayRunnable = new DisplayRunnable();
        Thread thread = new Thread(displayRunnable);
        thread.start();
    }

    /*
     * When an object implementing interface Runnable is
     * used to create a thread, starting the thread causes
     * the object's run method to be called in that
     * separately executing thread.
     */
    @Override
    public void run()
    {
        System.out.println("Hello Peter..");
    }
}
Output
Hello Peter..

Refer: 
https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/Thread_create_Runnable_app.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/Thread_create_Runnable_app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5edb9b935a4ef60d2a672d03f038390cd08d4eb7/BasicJava/Thread_create_Runnable_app/?at=master

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 (How to create a thread in java | Thread creation in java)

    Click here to watch in Youtube : 

    Click the below Image to Enlarge
    Java Tutorial : Java Threads (How to create a thread in java | Thread creation in java) 
    Java Tutorial : Java Threads (How to create a thread in java | Thread creation in java) 
    DisplayThread.java
    class DisplayThread extends Thread
    {
    
        public static void main(String args[])
        {
            /*
             * Allocates a new Thread object.
             */
            DisplayThread displayThread = new DisplayThread();
            /*
             * Causes this thread to begin execution; the Java
             * Virtual Machine calls the run method of this
             * thread
             */
            displayThread.start();
        }
    
        /*
         * Subclasses of Thread should override run() method.
         */
        @Override
        public void run()
        {
            System.out.println("Welcome to india");
        }
    }
    
    Output
    Welcome to india
    
    Refer: 
    https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Thread.html

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/Thread_create_ex_thread_app.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/Thread_create_ex_thread_app

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/5edb9b935a4ef60d2a672d03f038390cd08d4eb7/BasicJava/Thread_create_ex_thread_app/?at=master

    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
  • 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_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
  • 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
  • Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle) - Playlist

    Java Tutorial : Java Threads (Java Threading and multitasking) - Playlist

    Java Tutorial : Java Threads (Java processes and threads)-Playlist

    Java Tutorial : Java Threads (Java multithreading)- Playlist

    Java Threads Tutorial - Playlist

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

    Master Java Multithreading!

    Subscribe to our channel for step-by-step Core Java tutorials and concurrency deep dives!

    👉 Click Here to Subscribe

    Demystifying the Java Thread Life Cycle

    Multithreading is a core capability in Java that enables programs to execute multiple operations simultaneously. A thread progresses through a defined series of states throughout its execution. Gaining a clear understanding of these states is vital for writing safe, performant concurrent code.

    The Key Thread States Explained

    1. New State

    When a thread object is created using new MyThread(), it begins in the New state. It exists in memory but has not yet started running or interacting with the thread scheduler.

    2. Runnable State

    Calling the start() method transitions the thread into the Runnable state. In this state, the thread is ready to execute and waits in the pool for CPU resources allocated by the operating system.

    3. Running State

    When the thread scheduler picks the runnable thread, it enters the Running state and executes the code defined inside its run() method.

    4. Blocked / Waiting States

    Threads can temporarily pause their active execution:

    • Blocked: The thread is waiting to acquire a monitor lock to enter a synchronized block or method.
    • Waiting / Timed Waiting: Pauses execution until notified by another thread or until a sleep/join timer expires.

    5. Terminated (Dead) State

    When the run() method execution completes or an uncaught exception terminates the thread, it moves into the Terminated state and cannot be run again.

    Related Video Tutorials


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

    Click the below Image to Enlarge 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    Java Tutorial : Java Threads (Life cycle of a Thread in Java | Java thread life cycle_V2) 
    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
  • Java Tutorial : Java Threads (Java Threading and multitasking - Real time example)


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

    Click the below Image to Enlarge
    Java Tutorial : Java Threads (Java Threading and multitasking - Real time example) 
    Java Tutorial : Java Threads (Java Threading and multitasking - Real time example) 
    Java Tutorial : Java Threads (Java Threading and multitasking - Real time example) 
    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