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
"There is a will,there is a way!"<br />SCJP1.4
Joined: Jul 27, 2001
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.