This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes gives IllegalThreadStateException why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "gives IllegalThreadStateException why?" Watch "gives IllegalThreadStateException why?" New topic
Author

gives IllegalThreadStateException why?

pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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 ]

SCJP, SCWCD.
|Asking Good Questions|
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hiii

Thanks Amit and John.

Regards.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

You are welcome Pawni, its always a pleasure to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gives IllegalThreadStateException why?
 
Similar Threads
Question on Thread
regarding threads