从EDT调用invokeAndWait [英] calling invokeAndWait from the EDT
问题描述
我从以下问题previous问题。我也有code SwingUtillities.invokeAndWait
在code基别的地方,但是当我删除此GUI不刷新。如果我不将其删除我得到的错误是:
异常螺纹AWT-EventQueue的-0java.lang.Error:无法从事件调度线程中调用invokeAndWait
在了java.awt.EventQueue.invokeAndWait(来源不明)
在javax.swing.SwingUtilities.invokeAndWait(来源不明)
在game.player.humanplayer.model.HumanPlayer.act(HumanPlayer.java:69)
在HumanPlayer.act的code是:
公共动作ACT(动作决赛[] availiableActions){
尝试{ SwingUtilities.invokeAndWait(新的Runnable(){
@覆盖
公共无效的run(){
gui.update(availiableActions);
}
});
}
赶上(InterruptedException的E){
e.printStackTrace();
}赶上(的InvocationTargetException E){
e.printStackTrace();
} 同步(performedAction){
而(!hasPerformedAction()){
尝试{
performedAction.wait();
}赶上(InterruptedException的E){
e.printStackTrace();
}
}
setPerformedAction(假);
} 返回getActionPerfomed();
}
线程的图像时,在调试如屏幕不画:
堆栈的文字版:
ui.startup.LoginScreen位于localhost:51050
- >守护进程线程[AWT的窗口](运行)
- >螺纹[AWT关断](运行)
- >螺纹[AWT-EventQueue的 - 0](运行)
- >螺纹[DestroyJavaVM](运行)
答曰而不是使呼叫
新GameInitializer(用户名,播放器,Constants.BLIND_STRUCTURE_FILES.get(blindStructure),handState);
从EDT,使其执行上一个新的(非EDT)线程中,这样以后当 invokeAndWait
被人称其为正确充当运行该命令的线程不是在EDT。修订后的code是如下:
线程t =新主题(新的Runnable(){
@覆盖
公共无效的run(){
新GameInitializer(用户名,播放器,Constants.BLIND_STRUCTURE_FILES.get(blindStructure),handState);
} });
t.start();
I have a problem following from my previous problem. I also have the code SwingUtillities.invokeAndWait
somewhere else in the code base, but when I remove this the gui does not refresh. If I dont remove it the error I get is:
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)
The code in HumanPlayer.act is:
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();
}
Image of threads when in debug as screen doesn't paint:
Text version of stack:
ui.startup.LoginScreen at localhost:51050
-> Deamon Thread [AWT-Windows] (Running)
-> Thread [AWT-Shutdown] (Running)
-> Thread [AWT-EventQueue-0] (Running)
-> Thread [DestroyJavaVM] (Running)
The answer was instead of making the call
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
from the EDT, make it execute on a new (non EDT) thread so that later when invokeAndWait
is called it functions as correctly as the thread running that command is not the EDT. The amended code is as follows:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
}
});
t.start();
这篇关于从EDT调用invokeAndWait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!