Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Thread.stop() so dangerous

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.

like image 890
Dr.Threaduvelu Avatar asked Mar 07 '11 11:03

Dr.Threaduvelu


1 Answers

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:

  • didn't ever update data structures shared with other threads,
  • didn't use wait/notify or higher level synchronization classes, or classes that depended on them,
  • and possibly a few other things.

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.

like image 153
Stephen C Avatar answered Sep 24 '22 14:09

Stephen C