Threads

  • Java is a multithreaded programming language.This means that Our program can make optimal use of available resources by running two or more components concurrently , with each component handling a different task.
  • We can subdivide specific operations within a single application into individual threads that all run in parallel.
  • The following daigram shows the different cycle of thread.

.

  • There are two ways to create a threads.
  • Extends the Thread class –
  • Inherit from the Thread class, override its run() method , & write the functionality of the thread in run() method.
  • Then you create a new object of your class & call its start method to run the thread.
  • Example-
    class Loader extends Thread{
          public void run(){
                System.out.println("Hello");
       }
    }
    class Myclass{
          public static void main(String args[]){
                 Loader obj = new Loader();
                 obj.start();
        }
    }
    Output- 
    Hello

More on Threads