aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why Multiple invocation on start() on same thread instance causes Exception ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why Multiple invocation on start() on same thread instance causes Exception ?" Watch "why Multiple invocation on start() on same thread instance causes Exception ?" New topic
Author

why Multiple invocation on start() on same thread instance causes Exception ?

sainagaraju vaduka
Greenhorn

Joined: Feb 24, 2010
Posts: 1
Why does the multiple invocation of start on the same Thread instances gives us IllegalThreadStateException.
Why the JVM does not make us to start the same thread repeatedly....?
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi sainagaraju vaduka,

You can call start() more than one time, if the thread is alive or NOT dead.
You will get exception when you try to invoke a thread that already dead.


A.A.Anbarasu
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

anbarasu aladiyan wrote:
You can call start() more than one time, if the thread is alive or NOT dead.
You will get exception when you try to invoke a thread that already dead.



Actually no. A thread can only be started once. Period.

As for why, it is simply defined that way. I guess they could have done something like... ignore start() requests if it is already started or reinitialized everything, if the previous run has completed, but why? Why complicate matters? A thread is created, a thread is executed, and the thread object can be thrown away afterwards.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

We can call start() on a Thread object only once, whether is alive or dead, no matter!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Abimaran Kugathasan,

Check this code...



And re-read what Henry Wong said.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

anbarasu aladiyan wrote:Hi Abimaran Kugathasan,

Check this code...


And re-read what Henry Wong said.


Hi, anbarasu aladiyan.... Compile and run your coding......., You will get a RuntimeException, java.lang.IllegalThreadStateException... Because, the compiler only check the syntax of your coding. Logic of your coding can't be understand by the compiler. ... So It allows multiple invocation of start() method on a Thread. But, at runtime, the JVM check the logic of your coding, that's why it flags a runtime exception.... Hope you understand my last post reply.......
Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
A call to start method keeps that thread in queue for the scheduler to pick and run.
Now for the below code

In the first call the thread will be kept in the queue, now for the second call either main thread has to wait till the same existing thread object completes it's execution (as we can't have both in the queue as the object is same) or throw an exception.
They chose the second option.

This is just my analysis. There might be some other reasons also.


Thanks & Regards,
Srikanth Nalam.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Yes, Srikanth, to extend that thinking to an extreme case, the designers of Java did not want an infinite number of threads to be spawned and started if someone wrote really bad code such as this:


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Multiple invocation on start() on same thread instance causes Exception ?