wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes How to start a thread second time 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 » Java » Threads and Synchronization
Bookmark "How to start a thread second time " Watch "How to start a thread second time " New topic
Author

How to start a thread second time

Bhagirathi Mallick
Greenhorn

Joined: Jul 21, 2011
Posts: 26
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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: 26
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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to start a thread second time