从EDT调用invokeAndWait [英] calling invokeAndWait from the EDT
问题描述
我的以前的问题。我也在代码库中的其他地方的代码 SwingUtillities.invokeAndWait
,但是当我删除这个gui不刷新。如果我不删除它,我得到的错误是:
线程中的异常AWT-EventQueue-0java.lang.Error :无法从事件调度程序线程
调用invokeAndWait在java.awt.EventQueue.invokeAndWait(未知源)
在java.sql.SwingUtilities.invokeAndWait(未知源)
在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();
}
同步(performAction){
while(!hasPerformedAction()){
try {
performAction.wait();
} catch(InterruptedException e){
e.printStackTrace();
}
}
setPerformedAction(false);
}
return getActionPerfomed();
}
当屏幕不绘制时,调试时线程的图像:
alt text http://img684.imageshack.us/img684/6669/69288941.png
堆栈的文本版本:
ui。 startup.LoginScreen在本地主机:51050
- > Deamon线程[AWT-Windows](运行)
- >线程[AWT-Shutdown](运行)
- >线程[AWT-EventQueue-0](运行)
- >线程[DestroyJavaVM](运行)
发起呼叫
新的GameInitializer(userName,player,Constants.BLIND_STRUCTURE_FILES.get(blindStructure),handState);从$ EDT $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
被称为它正常运行该命令的线程不是EDT。修改后的代码如下:
code> invokeAndWait线程t = new Thread(new Runnable(){
@Override
public void run(){
new GameInitializer(userName,player,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: alt text http://img684.imageshack.us/img684/6669/69288941.png
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屋!