Canceling a thread

With the ability to end a thread externally, you can cancel threads that run long requests before they are completed on their own. You can use several APIs to cancel threads.

Some APIs provide well-defined points for the cancelation action to occur, or other mechanisms that allow one thread to control when another thread ends. Some APIs also provide a mechanism to run cleanup code before the thread ends, or to set the result of the canceled thread to a specific value.

Use thread cancelation carefully. If your API does not provide well-defined cancelation points or a mechanism for the thread to clean up the application data and locks, you might damage data or cause deadlocks within your application.

Note: In Java™ Development Kit version 1.2 or later, the resume, stop, and suspend methods for the thread class have been deprecated. This is because these methods are considered unsafe. The functions provided by these methods can be implemented by other mechanisms, such as checking the state of some variables. See the Sun Java tutorial for the suggested mechanism for a given version of Java.