Are Thread.stop and friends ever safe in Java?
Posted
by
Stephen C
on Stack Overflow
See other posts from Stack Overflow
or by Stephen C
Published on 2009-08-16T02:07:09Z
Indexed on
2010/12/26
18:54 UTC
Read the original article
Hit count: 221
java
|multithreading
The stop()
, suspend()
, and resume()
in java.lang.Thread
are deprecated because they are unsafe. The Sun recommended work around is to use Thread.interrupt()
, but that approach doesn't work in all cases. For example, if you are call a library method that doesn't explicitly or implicitly check the interrupted
flag, you have no choice but to wait for the call to finish.
So, I'm wondering if it is possible to characterize situations where it is (provably) safe to call stop()
on a Thread. For example, would it be safe to stop()
a thread that did nothing but call find(...)
or match(...)
on a java.util.regex.Matcher
?
(If there are any Sun engineers reading this ... a definitive answer would be really appreciated.)
EDIT: Answers that simply restate the mantra that you should not call stop()
because it is deprecated, unsafe, whatever are missing the point of this question. I know that that it is genuinely unsafe in the majority of cases, and that if there is a viable alternative you should always use that instead.
This question is about the subset cases where it is safe. Specifically, what is that subset?
© Stack Overflow or respective owner