FileDocCategorySizeDatePackage
ExtendThread.javaAPI DocExample1492Tue May 01 16:43:28 BST 2001None

ExtendThread.java

// Simple thread demonstration extending thread

class ExtendThread
  {
    public static void main(String args[])
      {
         NewThread et = new NewThread();  // create a new thread of execution object

         try
            {
              for(int i=5; i>0; i--)
                 {
                   System.out.println("Main Thread : " + i);
                   Thread.sleep(1000);  //put main thread to sleep yeild the CPU
                 }
             }
         catch(InterruptedException e)
             {
               System.out.println("Main thread interrupted.");
             }
         System.out.println("Main thread exiting.");
       }
   }


class NewThread extends Thread  //create a new thread class
  {

    NewThread()
      {
        super("extend thread demo");
        System.out.println( "Child thread: " + this);
        start();
      }

    // once a thread is started this is the entry point for subsequent time slices
    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 thread interrupted.");
             }
         System.out.println("exiting Child thread");
       }
}