在主方法中使用 SwingUtilities.invokeLater() [英] Using SwingUtilities.invokeLater() in main method

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

问题描述

最近看到一个MVC java应用,里面的main方法是这样写的:

I recently saw a MVC java application in which the main method was written as:

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                View view = new View();
                Model model = new Model();
                Controller controller = new Controller(view, model);
                controller.start();
            }
        });
    }

这不会使所有程序(包括模型和控制器,与 Swing 完全无关)一直运行到代码在 AWT 事件调度线程而不是主线程中结束?

Wouldn't this make all the program (including both the model and the controller, that have nothing to do with Swing at all) run until the code ends in the AWT Event Dispatch Thread instead of the Main thread?

如果最后一个是真的,那么这对应用程序来说真的很糟糕,因为它会阻止 EDT 执行它需要的任务(例如,调度事件,因为模型可能正在计算其他任务).正确吗?

If this last was true, then that would be really bad for the app as it would block the EDT from carrying out the tasks it needs to (dispatching events, for example, as the model could be calculating other tasks). Is it correct?

有一个类似的旧帖子(不是从这个重复)可以表明上面提到的代码是一种很好的做法,所以它让我更加困惑.

There is a similar old post (not a duplicate from this one) that can suggest the code mentioned above is good practice, so it confused me even more.

推荐答案

您展示的代码片段的目的是创建 Swing UI 和模型并将它们连接在一起.

The purpose of the code fragment that you show is to create the Swing UI and the model and to connect them together.

没有 Swing 更新(就对用户输入的反应而言),因为在 run() 方法结束之前不能有任何用户输入.

There is no Swing update (in terms of reacting to user input) since there cannot be any user input before the run() methods ends.

虽然您可以在主线程和 EDT 之间拆分这些任务(并且在第一次显示 UI 之前可能会获得几毫秒的时间),但它也会使应用程序的设计复杂化(多线程不是一个简单的话题)并且将代码库乱扔垃圾invokeLater() 调用.除非有人证明这是必要的,否则我不会这样做.

While you could split these tasks between main thread and EDT (and possibly gain a few milliseconds until the UI is first shown) it would also complicate the design of the application (multithreading is no easy topic) and litter the code base with invokeLater() calls. I would not do it until someone proves it to be necessary.

恕我直言,EDT 任何 GUI 应用程序中的主线程.对用户输入的每一次反应都从这个线程开始,UI 的每一次更新都必须在这个线程中完成.

IMHO the EDT is the main thread in any GUI application. Every reaction to user input starts in this thread and every update of the UI must be done in this thread.

长时间运行的任务应该在后台线程中完成 - 这通常意味着需要超过几毫秒的时间.

Long running tasks should be done in a background thread - which usually means anything that takes more than a few milliseconds.

如果创建模型需要几秒钟怎么办?

在那种情况下,我会尝试将模型创建分为两部分:

In that case I would try to split the model creation into two parts:

  • 创建所需的最小部分,以便可以显示 UI.这应该在 EDT 中完成(因为用户无论如何都必须等待这部分的完成 - 在显示 UI 之前,他无法与之交互)
  • 在后台线程中完成剩余的、长时间运行的部分.

如果不能这样做怎么办?(即在模型完全初始化之前无法显示 UI)

What if this cannot be done? (i.e. the UI cannot be displayed until the model is fully initialized)

在这种情况下,用户必须等待模型的完全初始化,然后才能看到和使用 UI.所以这个初始化是在 EDT 还是在主线程上运行都没有关系.因此,请使用更简单的解决方案:EDT 上的所有内容.

In this case the user has to wait for the complete initialization of the model before he can see and use the UI anyway. So it doesn't matter whether this initialization runs on the EDT or the main thread. So use the simpler solution: everything on the EDT.

但是通过显示 启动画面

这篇关于在主方法中使用 SwingUtilities.invokeLater()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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