How run() and start() methods works in Thread.









 This    java  example  gives  how  run() and start() methods  works . The  Thread behaves
 changes according  to   use  of  these methods.  code  is simple


class StartRun extends Thread{ 

       public void run(){ 
    for(int i=1;i<10;i++){ 
      try{Thread.sleep(2000);}
    
      catch(InterruptedException e)

      {System.out.println(e);}

      System.out.print(i); 
     } 
    } 

   } 


  public class ThreadRun {

   public static void main(String[] args) {
  // TODO Auto-generated method stub

          
            StartRun t1=new StartRun(); 
            StartRun t2=new StartRun(); 
       System.out.println("********Run Method************");
      System.out.println("********Run  running  supposing  simple  object not as Thread************");
             t1.run(); 
             t2.run(); 
          
             System.out.println("********Start Method************");
           
 System.out.println("* Start method  is running  supposing  two Thread objects************");
           
             t1.start();
             t2.start();
           
           }


           }

 This  is  output  of  the  program.

********Run Method************
********Run  running  supposing  simple  object not as Thread************
123456789123456789********Start Method************
* Start method  is running  supposing  two Thread objects************
112233445566778899








How run() and start() methods works in Thread. How  run()  and  start()   methods  works  in  Thread. Reviewed by Mukesh Jha on 10:55 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.