问题描述
请解释SwingUtilities中的invokeAndWait()方法。我无法理解这一点。
非常清楚地解释一下。 如果你尝试一个例子,那将会很有帮助。
Please explain invokeAndWait() method in SwingUtilities.I am unable to understand this.Explain it very clearly. It would be of great help if you try out with an example.
编辑添加@ noob扩展问题:
有什么不清楚?
这是一个经过修改的用法示例:
Here's a modified usage example:
import javax.swing.SwingUtilities;
public class InvokeAndWaitStuff
{
public static void main(String[] args)
{
final Runnable doHelloWorld = new Runnable() {
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doHelloWorld);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished on " + Thread.currentThread());
}
};
appThread.start();
}
}
输出:
Hello World on Thread[AWT-EventQueue-0,6,main]
Finished on Thread[Thread-0,5,main]
为什么这很重要?:
据我所知,这基本上是一个瓶颈,迫使GUI更新由单个线程同步执行,而不是由多个线程异步执行,这可能是不安全的。
As far as I can tell, this is basically a bottleneck that forces GUI updates to be executed synchronously by a single thread, rather than asynchronously by multiple threads, which can potentially be unsafe.
推荐答案
要了解 invokeAndWait()
的作用,首先需要了解Swing的事件/线程模型。
To understand what invokeAndWait()
does, you first need to understand the event/thread model of Swing.
基本上,以任何方式影响GUI的所有内容都必须在单个线程上发生。这是因为经验表明多线程GUI是不可能正确的。
Basically, everything that affects the GUI in any way must happen on a single thread. This is because experience shows that a multi-threaded GUI is impossible to get right.
在Swing中,这个特殊的GUI线程称为Event Dispatch Thread,或EDT 。它会在显示Swing顶级组件时启动,并且基本上是一个工作线程,它具有一个接一个地执行的事件对象的FIFO队列。
In Swing, this special GUI thread is called the Event Dispatch Thread, or EDT. It is started as soon as a Swing top-level component is displayed, and it's bascially a worker thread that has a FIFO queue of event objects that it executes one after another.
当需要绘制或更新Swing GUI时,JRE会在EDT队列上放置一个事件。导致侦听器被调用的用户操作作为EDT队列上的事件启动。并且(这是重要的部分)程序所做的一切都会改变GUI(比如注册监听器,添加/删除GUI组件或更改GUI显示的模型数据)必须放在EDT队列中,或者GUI可以获得损坏。
When a Swing GUI needs to be drawn or updated, the JRE places an event on the EDT queue. User actions that cause listeners to be called start as events on the EDT queue. And (this is this is the important part) everything your program does that changes the GUI (like registering listeners, adding/removing GUI components or changing model data that the GUI displays) must be placed in the EDT queue, or the GUI can get corrupted.
现在结束: invokeAndWait()
放置 Runnable
你将它传递到EDT事件队列并等待直到EDT执行它。当非GUI线程需要做一些影响GUI的事情时,应该使用它。还需要等到实际完成才能继续。如果你只是想做一些影响GUI的事情,但在完成时却不在乎,你应该使用 invokeLater()
。
And now for the finish: invokeAndWait()
places the Runnable
you pass to it into the EDT event queue and waits until the EDT has executed it. This should be used when a non-GUI thread needs to do something that affects the GUI, but also needs to wait until it is actually done before it can continue. If you just want to do something that affects the GUI but do not care when it is finished, you should instead use invokeLater()
.
这篇关于SwingUtilities中的invokeAndWait方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!