Thursday, March 5, 2015

New Thread Using Runnable in java pgm



Create New Thread Using Runnable


public class CreateThreadRunnableExample implements Runnable
{
public void run()
{
for(int i=0; i < 5; i++)
{
System.out.println("Child Thread : " + i);
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Child thread finished!");
}
public static void main(String[] args)
{
 Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
 t.start();               
for(int i=0; i < 5; i++)
{
System.out.println("Main thread : " + i);                  
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
  }
}
System.out.println("Main thread finished!");
  }
}

No comments:

Post a Comment