• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....?
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran Kugathasan,

Check this code...



And re-read what Henry Wong said.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic