Why is Thread.stop () so dangerous?

Why is Thread.stop() so dangerous?

Why is it recommended to use Thread.interrupted() instead?

I know stop deprecated. What else makes it unsafe?

Is there a place where I can use the stop method? If so, give me an example.

+5
source share
1 answer

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 recommended to use Thread.interrupted () instead?

Because Thread.interrupt() allows the target interrupt thread to respond when it reaches a point, when it knows that it is safe.

I know the stop is out of date. What else makes it unsafe?

See the link above.

Is there a place where I can use the stop method? If so, give me an example.

In theory, if you knew the thread you were stopping:

  • never updated data structures shared by other threads,
  • did not use wait / notification or higher level classes or classes depending on them,
  • and maybe 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 difficult to do an analysis to find out if the stop() call will actually be safe. For example, you need to analyze every possible bit of code (including main and third-party libraries) that the stream uses. Thus, this makes it unsafe by default.

+8
source

Source: https://habr.com/ru/post/1013733/


All Articles