how to detect if another java thread is suspended

2019-09-20 04:17发布

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条回答
别忘想泡老子
2楼-- · 2019-09-20 04:52

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.

查看更多
登录 后发表回答