Why is Thread.stop()
so dangerous?
Why is it advisable to use Thread.interrupted()
instead?
I know stop
is deprecated. What other thing makes it unsafe?
Is there any place where I can use stop
method? If so give me an example.
Why is Thread.stop() so dangerous?
The problems are described in detail here: http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
Why is it advisable to use Thread.interrupted() instead?
Because Thread.interrupt()
lets the target thread of the interrupt respond when it reaches a point when it knows it is safe to do so.
I know stop is deprecated. What other thing makes it unsafe?
See link above.
Is there any place where I can use stop method? If so give me an example.
In theory, if you knew that the thread you were stopping:
For example, I think it would be safe to stop()
this thread:
new Thread(new Runnable(){
public void run(){for (long l = 0; l > 0; l++){}}).start();
However, in most cases it is too difficult to do the analysis to figure out if calling stop()
will really be safe. For instance, you have to analyse every possible bit of code (including core and 3rd-party libraries) that the thread uses. So that makes it unsafe by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With