how to detect if another java thread is suspended or frozen?

给你一囗甜甜゛ 提交于 2019-12-12 02:04:29

问题


I searched and found ways to determine if another thread is suspended by adding a boolean to it. But, I do not want (prefer not) to modify that other thread's code. And, I need to detect if it is frozen by any reason (even a bug, so in this case that other thread may also be unresponsive).

I see how eclipse does it and shows the application's thread is not responding, I wonder how they coded that.

Motivation: I need this to release the mouse (ungrab it) from a 3D lwjgl application, and this must be independent of any IDE, the application itself must do it. I am trying to monitor the main application thread.


回答1:


While not necessarily the best idea, you can use Thread#getState() to view the current state of the thread.

However, this will depend on the context of the thread. If it is frozen because it cannot break out of a loop, then this method cannot work. Additionally, if it is simply waiting on a lock or on a bounded collection, it will be a false call.

The thread should in these 3 states: BLOCKED, WAITING, or TIMED_WAITING.



来源:https://stackoverflow.com/questions/28618823/how-to-detect-if-another-java-thread-is-suspended-or-frozen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!