我可能无意中更改了项目中的某些内容。现在,一个关键线程在不应该被中断的时候被中断,并且当线程应该运行时 isInterrupted
返回 true。
因为没有人会帮我找到问题,所以我寻求帮助来找到它。我需要找到:
- 最多是线程实际被中断的位置,并在程序运行时在控制台中输出
- 或者代码中所有被打断的地方,一一注释掉
因为 IDE 工具和调试器可能在我的搜索中发挥作用,所以我将补充说我正在使用 NetBeans IDE。
最佳答案
您可以在一个特殊的线程下运行它,该线程在中断时记录堆栈:
// A simple process.
class Process implements Runnable {
@Override
public void run() {
try {
while (true) {
Thread.sleep(1000L);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}
}
public void test() throws InterruptedException {
Thread t = new Thread(new Process()) {
@Override
public void interrupt() {
// Log a stack trace when iterrupted.
new Exception("Where the hell did that come from!").printStackTrace(System.out);
// Pass it up the chain.
super.interrupt();
}
};
t.start();
Thread.sleep(2048);
t.interrupt();
t.join();
}
关于java - 我怎样才能找到我的线程在哪里被中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181715/