• Post Reply Bookmark Topic Watch Topic
  • New Topic

stop a thread  RSS feed

 
Roger Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all
Suppose that you would like to stop a thread gracefully and release any locks that the thread
might be holding. Which of the following three techniques is preferred over the other two?
a. Invoke the Thread.stop method.
b. Invoke the Thread.destroy method.
c. Return from the run method based on the state of a boolean flag.
C is prefer. Who can give me a full explaination about it, specially that boolean flag?
Thanks
Roger
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For information one what's dangerous about destroy() and stop(), you need only consult the api docs.
Typically people will write a stop method that sets a boolean flag and then calls Thread.interrupt() in order to insure the a thread is not blocking (in an IO method for example) and knows that it should stop as soon as possible. If a thread is prematurely halted without a flag, it could stop in the middle of *any* operation. Quite often in code, immediately halting mid-operation will leave the program in an inconsistant state.
While there are scenarios where immediate halting may be perfectly safe, those scenarios are so rare as to cause software engineers to institutionalize a general disregard for stop() and any varients thereof.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!