如何检测另一个java线程是否挂起或冻结

如何检测另一个java线程是否挂起或冻结

本文介绍了如何检测另一个java线程是否挂起或冻结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并找到了通过向其添加布尔值来确定另一个线程是否挂起的方法.但是,我不想(不想)修改其他线程的代码.而且,我需要检测它是否因任何原因被冻结(即使是一个错误,所以在这种情况下其他线程也可能没有响应).

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).

我看到 eclipse 是如何做到的,并显示应用程序的线程没有响应,我想知道他们是如何编码的.

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

动机:我需要它来从 3D lwjgl 应用程序释放鼠标(松开它),这必须独立于任何 IDE,应用程序本身必须这样做.我正在尝试监视主应用程序线程.

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.

推荐答案

虽然不一定是最好的主意,但您可以使用 Thread#getState() 查看线程的当前状态.

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.

线程应该处于这 3 种状态:BLOCKED, WAITINGTIMED_WAITING.

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

这篇关于如何检测另一个java线程是否挂起或冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:25