Java Event-Dispatching Thread解释 [英] Java Event-Dispatching Thread explanation

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

问题描述

我最近开始学习和探索 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.

我的问题是基于这段代码:

My question is based on this piece of code:

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

我已经研究了一段时间,因为我想完全理解这段奇怪的代码,并且多次遇到术语事件调度线程".如果我错了,请纠正我,但据我所知;它与使用多线程以及 Java Swing 如何解释这些线程有关.我还认为上面的代码用于在创建窗口之前确保所有线程都是安全的",因此调用了 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"

并且只有在某些情况下才能从主方法调用对框架进行操作的方法.

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

有人可以向我澄清事件调度线程到底是什么吗?

它与多线程执行有何关系,以及从 main 方法调用这些线程如何不安全?还有为什么我们需要这个 invokeLater?

我们不能像创建任何其他对象一样创建窗口吗?

我在研究中遇到了一些障碍,因为我没有掌握这些关系和想法.

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

附带说明是,我喜欢将我的知识建立在深入理解的基础上,因为我相信这会带来最佳的整体结果,从而产生最佳的程序.如果我深入了解某些东西的工作原理,那么您就可以有效地使用这些提示和调整,而不仅仅是将它们重新编入代码中,所以请不要害怕给我一些更深入的解释并拓宽我的知识面.

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.

谢谢.

推荐答案

事件调度线程是一个由AWT管理的特殊线程.基本上,它是一个无限循环运行的线程,处理事件.

The event dispatch thread is a special thread that is managed by AWT. Basically, it is a thread that runs in an infinite loop, processing events.

java.awt.EventQueue.invokeLaterjavax.swing.SwingUtilities.invokeLater 方法是一种提供将在事件队列上运行的代码的方法.编写在多线程环境中安全的 UI 框架非常困难,因此 AWT 作者决定他们只允许在单个特殊线程上发生对 GUI 对象的操作.所有事件处理程序都将在此线程上执行,所有修改 GUI 的代码也应在此线程上运行.

The java.awt.EventQueue.invokeLater and javax.swing.SwingUtilities.invokeLater methods are a way to provide 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 框架会这样做),这意味着可以编写大量代码并且对此几乎不可知,而不会遇到任何问题.但这会导致未定义的行为,因此最好的做法是始终确保 GUI 代码在事件调度线程上运行.invokeLater 提供了一种机制来做到这一点.

Now AWT does not usually check that you are not issuing GUI commands from another thread (The WPF framework for C# does do this), meaning it's 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 dispatch thread. invokeLater provides a mechanism to do this.

一个经典的例子是你需要运行一个长时间运行的操作,比如下载文件.所以你启动一个线程来执行这个操作,当它完成时,你使用 invokeLater 来更新 UI.如果您没有使用 invokeLater 而是直接更新 UI,您可能会遇到竞争条件并可能发生未定义的行为.

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 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.

维基百科有更多信息

此外,如果您想知道为什么 AWT 作者不只是使工具包成为多线程的,这里 是一篇好文章.

Also, if you are curious why the AWT authors don't just make the toolkit multithreaded, here is a good article.

这篇关于Java Event-Dispatching Thread解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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