aspose file tools
The moose likes Threads and Synchronization and the fly likes stop a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "stop a thread " Watch "stop a thread " New topic
Author

stop a thread

Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
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
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: stop a thread
 
Similar Threads
Threads
Removing objects (Threads)
Can one thread halt another thread?
How can I really stop/kill a thread?
Master Vs Slave (MainMethods)