SEMrush

How to create a Thread ? Extend Thread Or Implement Runnable

Create a Thread

 1.  By Extending Thread Class
 


class IExtendThread extends Thread{ 

public void run(){  
System.out.println("I extended Thread Class, I am running now");  
}  

public static void main(String args[]){  
IExtendThread  ObjIExtendThread  = new IExtendThread  ();  
ObjIExtendThread .start();  
 }  

}  

           
                   Result: I extended Thread Class, I am running now


2.  By implementing Runnable interface
 




class IImpRunnable implements Runnable{  

public void run(){  
System.out.println("I implemented runnable, I am running");  
}  
  
public static void main(String args[]){  
IImpRunnable  ObjIImpRunnable  =new IImpRunnable();  
Thread t1 =new Thread(m1);  
t1.start();
          }  

}  


                    

                      Result : I implemented runnable, I am running





Comments