Java的事件派发线程解释 [英] Java Event-Dispatching Thread explanation

查看:964
本文介绍了Java的事件派发线程解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习和探索的Java GUI编程的基础知识。

I've recently started learning and exploring the basics of GUI programming in Java.

已经编写了一段时间我只是做后端的工作,或工作,因此我已经得到了用户界面的最接近的是命令控制台(尴尬的我知道)。

Having been programming for a while I have only done backend work or work and as a result the closest I've gotten to user interfaces is the command console (embarrassing I know).

我使用Swing和据我所知,这意味着由我也使用AWT扩展。

I'm using Swing and as far as I can gather that means by extension I am also using AWT.

我的问题是基于这块code的:

My question is based on this piece of code:

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        new frame.setVisible(true);
    }
} );

我一直在研究这一段时间,因为我想完全理解这种奇怪的一块code和所遇到的术语'事件派发线程多次。纠正我,如果我错了,但我的理解;它与使用多线程以及如何Java Swing的跨$ P $点这些线程做。我收集以及与上述code是用来确保所有的线程都是'安全'它创建的窗口前,因此invokeLater的?

I have been researching this for a while as I wanted to fully understand this strange piece of code and have come across the term 'Event-Dispatching Thread' multiple times. Correct me if I'm wrong but as I understand it; it has to do with using multiple threads and how Java Swing interprets those threads. I gather as well that the above code is used to make sure all the threads are 'safe' before it creates the window, hence the invokeLater?

我已阅读:

你只能调用从事件派发线程框架上进行操作的方法

"You can only call methods that operate on the frame from the Event-Dispatching Thread"

和只在某些情况下,你可以调用从main方法的框架上进行操作的方法。

and that only under certain circumstances can you call methods that operate on the frame from the main method.

能否有人请澄清我正是事件派发线程是什么?

它与多线程执行和这些线程都不怎么安全地从主方法叫什么?此外为什么我们需要这样的invokeLater?

难道我们不能只是创建窗口,任何其他对象?

我已经打了一下路障在我的研究,我没有把握这些关系和想法。

I've hit a bit of a road block in my research as I'm not grasping these relations and ideas.

一个侧面说明的是,我想立足于深入了解我的知识,因为我相信这将导致最佳的整体结果,结果的最佳方案。如果我深入的东西是如何工作的,那么你可以有效地使用技巧和调整,而不是人云亦云它们放回至code明白了,所以请不要害怕给我一些额外的深入解释,开拓了我知识。

A side note is that I like to base my knowledge on in-depth understanding as I believe this leads to the best overall outcome and as a result the best programs. If I understand in-depth how something works then you can use the tips and tweaks effectively rather than just parroting them back in to code, so please don't be afraid to give me some extra in-depth explanations and broaden my knowledge.

感谢您。

推荐答案

的EventDispatching螺纹是由AWT管理的特殊螺纹。基本上它是一个无限循环处理事件运行的线程。该方法了java.awt.EventQueue.invokeLater是一种特殊的方式来提供一些code,将在事件队列运行。编写一个UI框架,在多线程环境安全是非常困难的所以AWT作者决定,他们将只允许在GUI对象上的操作对单个特殊的线程发生。所有的事件处理程序将执行在此线程和所有code,它修改了GUI也应该在此线程运行。

The EventDispatching thread is a special thread that is managed by the AWT. Basically it is a thread that runs in an infinite loop processing event. The java.awt.EventQueue.invokeLater method is a special way to provide some code that will run on the event queue. Writing a ui framework that is safe in a multithreading environment is very difficult so the AWT authors decided that they would only allow operations on GUI objects to occur on a single special thread. All event handlers will execute on this thread and all code that modifies the gui should also operate on this thread.

现在的AWT通常不会检查你是不是从另一个线程的问题GUI命令(C#中的WPF框架确实做到这一点)。因此可以写很多code,并且是pretty太多不可知的这种不遇到任何问题。但是,这可能会导致不确定的行为,做的最好的事情是要始终确保事件调度线程上的GUI code运行。的invokeLater提供一种机制来做到这一点。

Now the AWT does not usually check that you are not issues gui commands from another thread (The WPF framework for C# does do this). so it is possible to write a lot of code and be pretty much agnostic to this and not run into any problems. But this can lead to undefined behavior so the best thing to do is to always ensure that gui code runs on the event dispatcher thread. invokeLater provides a mechanism to do this.

因此​​,一个典型的例子是,你需要运行像下载文件长时间运行的操作。所以,你启动一个线程,当它完成后,你将使用的invokeLater更新UI来,然后执行此操作。如果您没有使用的invokeLater,而是刚刚更新的用户界面直接和您可能有一个竞争条件和不确定的行为可能会发生。

So a classic example is that you need to run a long running operation like downloading a file. So you launch a thread to perform this action then when it is completed you will use invokeLater to update the UI. If you didn't use invokeLater and instead you just updated the ui directly you might have a race condition and undefined behavior could occur.

维基百科有更多信息:<一href=\"http://en.wikipedia.org/wiki/Event_dispatching_thread\">http://en.wikipedia.org/wiki/Event_dispatching_thread

此外,如果你很好奇,为什么AWT作者不只是在这里做多线程工具包是一个很好的文章:<一href=\"http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html\">http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html

Also if you are curious why the awt authors don't just make the toolkit multithreaded here is a good article: http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html

这篇关于Java的事件派发线程解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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