我使用一个actionListener来触发一系列事件和最后通牒--这段代码被称为:
public class ScriptManager {
public static Class currentScript;
private Object ScriptInstance;
public int State = 0;
// 0 = Not Running
// 1 = Running
// 2 = Paused
private Thread thread = new Thread() {
public void run() {
try {
currentScript.getMethod("run").invoke(ScriptInstance);
} catch(Exception e) {
e.printStackTrace();
}
}
};
public void runScript() {
try {
ScriptInstance = currentScript.newInstance();
new Thread(thread).start();
State = 1;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseScript() {
try {
thread.wait();
System.out.println("paused");
State = 2;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public void resumeScript() {
try {
thread.notify();
System.out.println("resumed");
State = 1;
MainFrame.onResume();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopScript() {
try {
thread.interrupt();
thread.join();
System.out.println("stopped");
State = 0;
MainFrame.onStop();
} catch (Exception e) {
e.printStackTrace();
}
}
}runnable是创建并运行的,但是,当我尝试使用其他方法时,它们会锁定我的UI,问题就会发生。(我假设这是因为我在EDT上运行这个程序)有人知道如何解决这个问题吗?
发布于 2013-11-09 20:51:00
这不是你使用wait和notify的方式。它们需要在您试图暂停和恢复的线程上执行。这意味着您需要以某种方式向另一个线程发送消息。这样做有很多种方法,但另一个线程需要侦听这条消息,或者至少偶尔检查一下。
https://stackoverflow.com/questions/19882870
复制相似问题