Search This Blog

Wednesday, 28 January 2015

Write a program to create a thread that implements the runnable interface



Class  FirstThread implements Runnable
{  
  public void run()    
  {                
    for ( int i=1; i<=10; i++)      
    {                     
        System.out.println( "Messag from First Thread : " +i);                     
          try           
        {              
           Thread.sleep (1000);            
        }          
        catch (Exception e)          
        {                        
            System.out.println( e);          
        }  
    }  
  }
} 
class  SecondThread implements Runnable
{ 
   public void run()   
   {          
      for ( int i=1; i<=10; i++)        
      {            
         System.out.println( "Messag from Second Thread : " +i);                
         try          
         {             
             Thread.sleep(1000);           
         }         
         catch (Exception e)         
         {                                 
             System.out.println(e);            
         }     
      }    
    } 
}
class Thd
{
     public static void main(String args[])
     {
        FirstThread   firstThread = new FirstThread();
        SecondThread   secondThread = new SecondThread();
         Thread thread1 = new Thread(firstThread);
        thread1.start();
        Thread thread2 = new Thread(secondThread);
        thread2.start();
     }
}



                           OUTPUT




No comments:

Post a Comment