Sunday, January 12, 2014

Problem if you direct call run() method in Threading.

class Thread16 extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(" "+i);
}
}
public static void main(String surat[])
{
Thread16 t1=new Thread16();
Thread16 t2=new Thread16();
 
t1.run();
t2.run();
}
}

Output:


As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not thread object.

No comments:

Post a Comment