Java 线程如何与 invokeLater() 同步? [英] How does a Java thread synchronize with invokeLater()?
问题描述
我有一个非 GUI 线程,它使用
I have a non-GUI thread that starts a JFrame using
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
cardReadPunchGUI = new IBM1622GUI(); // instantiate
cardReadPunchGUI.setVisible(true);
}
});
IBM1622GUI 的构造函数的一部分为自己实例化了一个模型",我的非 GUI 线程需要访问它:
Part of IBM1622GUI's constructor instantiates a "model" for itself, which my non-GUI thread needs access to:
cardReadPunch = IBM1622GUI.getModel();
我的非 GUI 线程与稍后调用"的新 GUI 同步的正确方法是什么?(当然,如果没有同步,IBM1622GUI.getModel()
只会返回 null.)
What is the correct way for my non-GUI thread to synchronize with the new GUI that's been "invoked later"? (Without synchronization, of course, IBM1622GUI.getModel()
just tends to return null.)
推荐答案
使用
javax.swing.SwingUtilities.invokeAndWait(Runnable doRun);
相反.
导致 doRun.run() 在 AWT 事件上同步执行调度线程.此调用会阻塞,直到所有挂起的 AWT 事件都具有被处理并且(然后) doRun.run() 返回.
Causes doRun.run() to be executed synchronously on the AWT event dispatching thread. This call blocks until all pending AWT events have been processed and (then) doRun.run() returns.
这篇关于Java 线程如何与 invokeLater() 同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!