Multithreading

Creating a thread

Two ways-

  1. extend Thread class
  2. implement Runnable interface

Implement the run() method -> this is the code which will get executed when the thread runs.

Executing Threads

  • Call start() method

    The part after start method call is executed immediately. If you want to execute some part of the code after the thread execution, use join() method.

        Thread thread = new ApnaThread();
    
        thread.start();
    
        //this part gets executed immediately after start() is called - while the thread is running
    
        thread.join();
    
        //this part gets executed after the thread execution is completed.
    
  • Use ExecutorService

Thread Execution

Java Thread Life Cycle

flowchart LR subgraph Thread New--start called--> Runnable Runnable-->Running Running --run method exits or stop method called-->Terminated end subgraph Blocked Code Non-runnable --sleep done, I/O complete, lock available, resume, notify, notifyAll--> Runnable Running --sleep, block on I/O, wait for lock, suspend, wait--> Non-runnable end

Thread Scheduler

Task executes for a predefined slice of time and then re-enters pool of ready tasks.

Next task decided based on priority and other factors.

Uses Pre Emptive Scheduling and Time Slicing


Children
  1. Deadlocks
  2. Thread Safety