Friday 29 June 2012

java program to create Thread by using Runnable interface

// controlling the main thread
class mynewthread implements Runnable
{
  
      Thread t;
      mynewthread()
      {
         // create new thread
         t= new Thread(this,"demo thread");
         System.out.println("child thread :"+t);
         t.start();
      }
      public void run()
       {
         try
         {



            for( int i=5;i>0;i--)
             {
               System.out.println("child thread :"+i);
               Thread.sleep(500);
             }
          }catch ( InterruptedException e)
          {
            System.out.println("child interrupted :");
          }
          System.out.println("exiting child thread ");
       }// run()
 } // mynewthread class
 class mythread
  {
    public static void main(String args[])
     {
       new mynewthread();
       try
       {
            for( int i=5;i>0;i--)
             {
               System.out.println("main thread :"+i);
               Thread.sleep(500);
             }
       }catch ( InterruptedException e)
          {
            System.out.println("main interrupted :");
          }
      System.out.println("exiting main exiting ");
    } // main

  } // java program          

No comments:

Post a Comment