Wednesday, February 25, 2009

source code to demonstrate thread sleep method



package thread;

class MyTh extends Thread
{
public void run()
{
for(int i=0;i<3;i++)
{
System.out.println("child thread");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}

}
public class ThreadDemo2
{
public static void main(String a[])throws Exception
{
MyTh t=new MyTh();
t.start();
for(int i=0;i<2;i++){
System.out.println("main thread");
}
}
}

output:
mainthread
child thread
mainthread
childthread
child thread


0 comments:

Post a Comment