Tuesday 8 April 2014

Creating a thread by using Runnable Interface

Creating a thread by using Runnable Interface


class Thread_demo implements Runnable
{
  String str,msg;
  Thread t;
  Thread_demo(String str)
  {
   msg=str;
   t=new Thread(this,str);
   t.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_runnable
{
  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...