简体   繁体   中英

Java: Thread.stop thread without monitors

Java's Thread.stop is unsafe because : "Thread.stop causes it to unlock all of the monitors that it has locked"

This makes sense. But, what if that thread is not holding any monitors set in application code, is it safe to use Thread.stop? I understand it is bad practice to use Thread.stop, but it is not clear to me why it would be unsafe if the running code does not hold monitors.

You may be thinking a bit too narrowly at the affected objects here. It can be any monitor that the thread touches (which is essentially just an object accessed by more than one thread), or any object (in some cases, possibly even primitives) referenced by any of those monitors. Any one of those may be left in a broken state, depending on when Thread.stop() is called, with no vaguely easy way of finding out what exactly is broken.

If your Thread really doesn't touch any such objects, then technically it's "safe" - but that would be a very rare case, and an odd one too (since if the thread never interacts with any kind of state that can be referenced anywhere else, what's the point of it?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM