【发布时间】:2015-09-25 13:04:00
【问题描述】:
我有一个java程序,编译需要很长时间。
出于测试目的,如果编译需要很长时间,我想终止程序并重新启动它。
这是我的代码的简化版本:
public class Main {
public static void main(String[] args) {
Thread foo = new Thread(new Foo());
while (true) {
foo.start();
while (true) {
if (needRestart()) {
foo.interrupt();
break;
}
}
}
}
}
foo.java 看起来有点像这样:
public class Foo implements Runnable {
// some code
public void run () {
try {
while (!Thread.currentThread().isInterrupted()) {
// some code
}
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
问题是程序崩溃并抛出IllegalThreadStateException
如果你需要完整的代码,这里是:full code
【问题讨论】:
-
编译或运行需要很长时间?
-
@ACV 编译需要很长时间,我在 EV3 上运行它
-
任何时候遇到异常,都应该在问题中包含其整个堆栈跟踪。
-
我认为这发生在这里
} catch(InterruptedException ex) { Thread.currentThread().interrupt(); },因为你试图中断一个被中断的线程。做一个ex.printStackTrace();
标签: java multithreading interrupt lejos-nxj thread-state