| Author |
How to start a thread second time
|
Bhagirathi Mallick
Greenhorn
Joined: Jul 21, 2011
Posts: 25
|
|
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
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
|
Once a thread has been started you cannot invoke start() on the same Thread instance.
|
Mohamed Sanaulla | My Blog
|
 |
Bhagirathi Mallick
Greenhorn
Joined: Jul 21, 2011
Posts: 25
|
|
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
Bartender
Joined: Aug 22, 2010
Posts: 2333
|
|
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.
|
 |
 |
|
|
subject: How to start a thread second time
|
|
|