Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to start a thread second time

 
Bhagirathi Mallick
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to start a new Thread by using the reflection mechanism
but i am getting Exception, please some one help by reading the following code




thanks in advance...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once a thread has been started you cannot invoke start() on the same Thread instance.
 
Bhagirathi Mallick
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi... Mohamed Sanaulla

Actually this is the internal code of start method :



with the help of reflection api i changed the value of threadStatus
then i called , now tell me why i can not call
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is "cannot" and "cannot". The other "cannot" actually means "shouldn't under any circumstances whatsoever".

By doing this you're violating the Thread class contract. Using reflection allows you to do a lot of things that designers of the Java API didn't foresee and can have unintended consequences. It is possible that a new thread was not actually started, or if it did start, it might operate incorrectly. ThreadLocal variables would quite certainly be messed up in that case, for example. And I would not be surprised if the JVM crashed at some point after doing this.

Why bothering starting the thread again? Creating a new instance is way easier, and infinitely safer.

By the way, using bold on your whole post makes it harder to read. Any emphasis or highlight is best used just on a few words you want to get more attention to. Anything else is quite counter-productive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic