Java 我怎样才能找到我的线程被中断的地方?
我可能无意中更改了项目中的某些内容。现在,一个键线程在不应该被中断时被中断,Java 我怎样才能找到我的线程被中断的地方?,java,multithreading,netbeans,Java,Multithreading,Netbeans,我可能无意中更改了项目中的某些内容。现在,一个键线程在不应该被中断时被中断,isInterrupted在线程应该运行时返回true 因为没有人会为我找到问题,我请求帮助找到问题的方法。我需要找到: 最好是线程实际中断的位置,并在程序运行时在控制台中输出它 或者在代码中被中断的所有地方,并逐个注释掉它们 由于IDE工具和调试器可能在我的搜索中发挥作用,我将补充说我正在使用NetBeans IDE。您可以在一个特殊线程下运行它,该线程在堆栈中断时记录堆栈: // A simple process
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();
}
您可以在中断堆栈时记录堆栈的特殊线程下运行它:
// 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();
}
Thread.interrupt()
是公共的-您可以覆盖它并进行堆栈跟踪。@OldCurmudgeon是curmudgeonly,它是公共的这一事实与此无关。重要的是它不是最终的。@EJP-你一如既往地正确。NetBeans:转到“导航”菜单->转到类型->java.lang.Thread->右键单击中断方法->选择“查找用法”。对java.util.concurrent.Future.cancel(true)重复上述操作。您可以中断Thread.interrupt()
方法,并查看其调用位置。Thread.interrupt()
是公共的-您可以覆盖它并进行堆栈跟踪。@oldcurmudgeonly是curmudgeonly,它是公共的事实是无关的。重要的是它不是最终的。@EJP-你一如既往地正确。NetBeans:转到“导航”菜单->转到类型->java.lang.Thread->右键单击中断方法->选择“查找用法”。对java.util.concurrent.Future.cancel(true)重复上述操作。您可以中断Thread.interrupt()
方法并查看调用它的位置。