我的previous problem有一个问题。我在代码库的其他地方也有代码SwingUtillities.invokeAndWait
,但是删除此gui不会刷新。如果我不删除它,我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
at game.player.humanplayer.model.HumanPlayer.act(HumanPlayer.java:69)
HumanPlayer.act中的代码是:
public Action act(final Action[] availiableActions) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
gui.update(availiableActions);
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
synchronized(performedAction){
while(!hasPerformedAction()){
try {
performedAction.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setPerformedAction(false);
}
return getActionPerfomed();
}
在屏幕调试中无法绘制线程的图像:
alt text http://img684.imageshack.us/img684/6669/69288941.png
堆栈的文本版本:
ui.startup.LoginScreen at localhost:51050
-> Deamon Thread [AWT-Windows] (Running)
-> Thread [AWT-Shutdown] (Running)
-> Thread [AWT-EventQueue-0] (Running)
-> Thread [DestroyJavaVM] (Running)
最佳答案
答案不是打电话
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
从EDT中执行,使其在新的(非EDT)线程上执行,以便稍后调用
invokeAndWait
时,它的功能与运行该命令的线程不是EDT一样正确。修改后的代码如下:Thread t = new Thread(new Runnable() {
@Override
public void run() {
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
}
});
t.start();
关于java - 从EDT调用invokeAndWait,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2435397/