gpt4 book ai didi

java - 从 EDT 调用 invokeAndWait

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:57 27 4
gpt4 key购买 nike

我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com