SwingUtilities 中的 invokeAndWait 方法 [英] invokeAndWait method in SwingUtilities

查看:36
本文介绍了SwingUtilities 中的 invokeAndWait 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释 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]

为什么这很重要?:

使 doHelloWorld.run() 成为在 AWT 上同步执行事件调度线程.这个电话阻塞直到所有挂起的 AWT 事件已经被处理并且(然后)doHelloWorld.run() 返回.这个方法应在以下情况下使用应用程序线程需要更新图形界面.

Causes doHelloWorld.run() to be executed synchronously on the AWT event dispatching thread. This call blocks until all pending AWT events have been processed and (then) doHelloWorld.run() returns. This method should be used when an application thread needs to update the GUI.

据我所知,这基本上是一个瓶颈,它迫使 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 线程称为事件调度线程,或 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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆