How to create a thread in java

Creating a Thread:

  1. write a class that extends Thread class or implements Runnable interface.
  2. write public void run() method in side the class. Note: Athread always execute the statements of public void run() only.
  3. Crating an object to the class.
  4. Create a thread and attach to the object.
  5. run the thread.

Example: 

// creating and running a Thread

class Myclass extends Thread

 {

  public void run()

  {

   for(int i=0; i<100000;i++)

    {

     System.out.println(i);

   }

   }

}

class TDemo

{

  public static void main(String[]args) hrows exception

  {

     Myclass obj = new Myclass();

   Thread t1 = new Thread (obj);

   t1.start();

 

  }

}