成都创新互联网站制作重庆分公司

为什么要加EventQueue.invokeLater呢

为什么要加EventQueue.invokeLater呢,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。十年网站建设经验成都创新互联是成都老牌网站营销服务商,为您提供成都网站制作、成都网站建设、外贸营销网站建设、网站设计、H5网站设计、网站制作、品牌网站建设、小程序开发服务,给众多知名企业提供过好品质的建站服务。

比如下面的程序:

import java.awt.*;

import javax.swing.*;

public class Test 

{

    public static void main(String[] args)

    {

        EventQueue.invokeLater(new Runnable()

            {

                public void run()

                {

                    JFrame frame = new JFrame();

                    frame.setSize(400, 300);

                    frame.setVisible(true);

                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                }

            });

    }

}



原因:
Java's GUI is strictly single-threaded.
All GUI related things in java should always go through a single thread. The thread is our legendary "AWT-EventQueue-0"   . Hence all GUI related actions should necessarily go through the AWT Event thread. If not so you may end up in a deadlock. For small programs, this might never happen. But for a huge java application if you try frame.setVisible(true) kind of thing in main thread, you will soon find yourself searching a new job. What invokeLater() does is to post your Runnable in the AWT thread's event queue. So the code in your run method will be executed in the AWT-Eventqueue thread.
大意是说,java的GUI都是的单线程,应该使用事件调度线程去执行,如果没意思使用事件调度线程的话,可能造成死锁。但是在小的程序中,这种现象(死锁)不会发生的;大的应用程序中才会出现这种现象!

关于为什么要加EventQueue.invokeLater呢问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章题目:为什么要加EventQueue.invokeLater呢
文章转载:http://cxhlcq.com/article/pojdhs.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部