我怎样才能找到我的线程在哪里中断?
How can I find where was my thread interrupted?
我可能无意中更改了项目中的某些内容。现在,一个关键线程在不应该中断时被中断,而当线程应该 运行.
时 isInterrupted
returns 为真
因为没有人会为我找到问题,所以我寻求帮助以找到它的方法。我需要找到:
- 充其量是线程实际中断的位置,在程序运行时在控制台输出运行
- 或者代码中所有被打断的地方都一一注释掉
因为 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();
}
我可能无意中更改了项目中的某些内容。现在,一个关键线程在不应该中断时被中断,而当线程应该 运行.
时isInterrupted
returns 为真
因为没有人会为我找到问题,所以我寻求帮助以找到它的方法。我需要找到:
- 充其量是线程实际中断的位置,在程序运行时在控制台输出运行
- 或者代码中所有被打断的地方都一一注释掉
因为 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();
}