Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

gives IllegalThreadStateException why?

 
pawni jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have written a code that gives output as..
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:595)
at javaapplication1.threads.go(threads.java:19)
at javaapplication1.threads.main(threads.java:6)
fooo



code is as follows..


package javaapplication1;
public class threads {
public static void main(String args[])
{
new threads().go();
}
public void go()
{
Runnable r=new Runnable()
{
public void run()
{
System.out.print("fooo");
}
};
Thread t=new Thread(r);
t.start();
t.start();

}

}



why this code giving illegalthreadstateexception ???
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pawni,

you cannot restart the thread once its finished, hence the exception.
the second t.start(); is wrong.
Remove it and you'll get rid of the exception.



Hope this helps
[ May 21, 2008: Message edited by: Amit Ghorpade ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the states which a thread can take in its life cycle and you will understand this problem better.

You cannot restart threads and you cannot start dead threads
 
pawni jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiii

Thanks Amit and John.

Regards.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Pawni, its always a pleasure to help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic