Click to See Complete Forum and Search --> : [RESOLVED] error with Thread


vaishnavi
04-13-2009, 04:02 AM
Hi all,

I have the following code, but it gives me a run time exception.

Here is my code:

public static void main(String args[])
{
new Threads4().go();
}
public void go()
{
Runnable r = new Runnable()
{
public void run()
{
System.out.print("foo");
}
};
Thread t=new Thread(r);
t.start();
t.start();
}
}

The run time exception which i am getting is:
Exception in Thread "main" java.lang.IllegalThreadStateException.

Actually it is printing the output "foo" and the above exception follows.

Can anyone tell me why am i getting this error?

chazzy
04-13-2009, 07:29 AM
you can't start a thread twice. you need to instantiate two different threads using the same target (r)

e.g.:


Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start();

vaishnavi
04-16-2009, 06:28 AM
chazzy,

Sorry for the late reply, but that really helped me in understanding the concept.:)