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


Children
  1. ExecutorService
  2. Runnable
  3. Thread

Backlinks