Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sainagaraju vaduka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can call start() on a Thread object only once, whether is alive or dead, no matter!
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abimaran Kugathasan,

Check this code...



And re-read what Henry Wong said.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic