Tuesday 8 April 2014

creating a three thread by extending the Thread class

 creating a three thread by extending the Thread class


/******************************************************
This is a Java program which creates a three thread
by extending the Thread class
*******************************************************/
class Thread_demo extends Thread
{
  String msg;
  Thread_demo(String str)
  {
   msg=str;
   start();//directs to the run method
  }
  public void run()//thread execution starts
  {
    for(int i=1;i<=5;i++)
    {
      System.out.println(msg);
    }
    try
    {
        Thread.sleep(1000);
    }
    catch(InterruptedException e)
    {
     System.out.println("Exception in Thread handling");
    }
   }
}
public class Many_Thread
{
  public static void main(String args[])
  {
    Thread_demo t1=new Thread_demo("First Thread");
    Thread_demo t2=new Thread_demo("Second Thread");
    Thread_demo t3=new Thread_demo("Third Thread");
  }
}

No comments:

Comment

Comment Box is loading comments...