简体   繁体   English

使用SwingUtilities.invokeLater()在线程中插入GUI的优点是什么?

[英]What are the advantages of luanching a GUI in a thread using SwingUtilities.invokeLater()

Hi all I have seen Swing GUI's being launch like this 大家好,我已经看到Swing GUI正在这样启动

public class Main {

public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            new Gui();
        }

    });

}

}

What are the benefits of launching it this way? 以这种方式启动它有什么好处?

More than benefits, I would say that it is necessary because most Swing components are not thread safe. 我要说的不仅仅是好处,因为大多数Swing组件都不是线程安全的。 invokeLater will run the task inside Swing event dispatch thread, avoiding thread interference or memory consistency errors. invokeLater将在Swing事件分配线程中运行任务,避免线程干扰或内存一致性错误。

You can find it mentioned in the Swing Tutorial, Event Dispatch Thread chapter . 您可以在Swing教程的“事件调度线程”一章中找到它。

Swing is single threaded. 秋千是单线程的。 You need to queue GUI changes onto the swing event dispatch thread. 您需要将GUI更改排队到swing事件分配线程上。 It isn't really an advantage, it is a requirement. 这并不是真正的优势,它是必要条件。 Otherwise, the behavior is undefined. 否则,行为是不确定的。

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM