• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

stop a thread

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic