Hi all,
When I try to run the code using t.start() the output is about both main
thread and the child thread. But when I try to run the code with t.run() then the out put is just about the child thread.
Can any one tell the reason why is this behaviour.What is the difference between calling t.start() or calling t.run() directly.
class counter implements Runnable
{
private int currentvalue;
private Thread t;
counter(
String threadname)
{
currentvalue=0;
t=new Thread(this,threadname);
System.out.println(t);
t.run();
}//this is for the constructor
public void run()
{
try
{
while(currentvalue<5)
{
System.out.println("The thread's name is"+t.getName()+":"+(currentvalue++));
t.sleep(500);
}
}catch(InterruptedException e)
{
System.out.println("Thread is interrupted");
}
System.out.println("The child thread exitted");
}
public int getValue()
{
return currentvalue;
}
}//this is for the counter class
public class client
{
public static void main(String [] args)
{
counter c=new counter("Child");
try
{
int val;
do
{
val=c.getValue();
System.out.println("The main thread's value is"+val);
Thread.sleep(1000);
}while(val<5);
}catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main exitted");
}//this is for the main method
}//this is for the class client
THanks & regards
Ananth