我目前正在使用别人编写的线程控制类,它用于一个Java Swing应用程序。我有两种方法,但我对为什么会有不同的行为感到困惑。根据我所知道和阅读关于事件分派线程和Swing的内容,下面的两种方法应该没有区别。显然,这是不正确的。
//If this is the AWT Event Processing thread then run the code immediately,
//otherwise schedule it for later processing
public static void runWithEventThread(Runnable r)
{
if (EventQueue.isDispatchThread())
{
r.run();
}
else
{
EventQueue.invokeLater(r);
}
}
//Schedule the runnable for later processing by the AWT Event Queue
public static void runLaterWithEventThread(Runnable r)
{
EventQueue.invokeLater(r);
}
当使用runWithEventThread()展示弹出框并更新GUI(添加新按钮/重绘)时,我发现GUI有时会出现混乱。然而,当使用runLaterWithEventThread()时,一切都很好,没有问题。
唯一的问题是,当使用runLaterWithEventThread()时,我发现当我有多个弹出窗口一个接一个地显示(在点击OK后),所有弹出窗口都会同时显示。
据我所知,这两种方法应该做的是相同的事情。请问有人能解释一下发生了什么吗?