什么是事件调度线程? [英] What is the event dispatching thread?

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

问题描述

我知道线程意味着什么,如果我将事件调度线程(EDT)理解为
只是一个线程,它解释了很多,但显然,它并不能解释所有事情。

I know what "thread" means and if I understand the event dispatching thread (EDT) as "just a thread", it explains a lot but, apparently, it does not explain everything.

我不明白这个帖子有什么特别之处。例如,我不明白为什么我们应该在EDT中启动GUI?为什么主线程适合GUI?好吧,如果我们只是不想占用主线程为什么我们不能在另一个线程中启动GUI为什么它应该是一些叫做EDT的特殊线程?

I do not understand what is special about this thread. For example I do not understand why we should start a GUI in a the EDT? Why the "main" thread is bed for GUI? Well, if we just do not want to occupy the main thread why we cannot start GUI just in "another thread" why it should be some "special" thread called EDT?

然后我不明白为什么我们不能像任何其他线程一样启动EDT?为什么我们应该使用一些特殊的工具(称为 invokeLater )。而且,与其他任何线程不同,GUI不能立即启动。我们应该等到它准备接受我们的工作。是因为EDT可以同时执行多项任务吗?

Then I do not understand why we cannot start the EDT like any other thread? Why we should use some special tool (called invokeLater). And why GUI, unlike any other thread, does not start immediately. We should wait until it is ready to accept our job. Is it because EDT can, potentially execute several task simultaneously?

如果你决定回答这个问题,你是否愿意使用一个非常简单的术语,否则,我担心,我将无法理解答案。

If you decide to answer this question, could you pleas use a really simple terminology because otherwise, I am afraid, I will not be able to understand the answer.

已添加:

I一直以为我们每个线程都有一个任务。因此,在每个线程中,我们执行预定义的命令序列。但在我看来,在事件调度线程中我们可以拥有服务器任务。好吧,它们不是同时执行的(线程在不同的任务之间切换,但在一个线程中仍然有几个任务)。这样对吗?例如,在EDT中有一个显示主窗口的线程,然后另外我们向EDT发送了另一个应该更新其中一个窗口组件的任务,EDT将在它准备就绪时执行这个新任务。 EDT是否与其他线程不同?

I always thought that we have one "task" per thread. So, in every thread we execute a predefined sequence of commands. But it seems to me that in the event dispatching thread we can have sever task. Well, they are not executed simultaneously (thread switches between different task but there are still several task in one thread). Is it right? For example there is one thread in the EDT which display the main window, and then additionally to that we sent to the EDT another task which should update one of the window components and EDT will execute this new task whenever it is ready. Is EDT differ from other threads in this way?

推荐答案

事件调度线程是处理所有GUI事件并管理您的事务的线程Swing GUI。如果您的程序中有任何GUI,它将在Swing代码中的某个位置启动。它是在幕后完成的原因是因为简单 - 您不必费心自己开始和管理额外的线程。

The event dispatching thread is the thread that handles all GUI events and manages your Swing GUI. It is started somewhere in the Swing code if you have any GUI in your program. The reason it is done behind the scenes is because of simplicity - you do not have to bother with starting and managing an extra thread by yourself.

关于你有这个事实使用 invokeLater()更新GUI是因为并发问题。 GUI只能从一个线程修改,因为Swing不是线程安全的(值得注意的是大多数的工具包不是线程安全的,有一个好文章给出了一些想法的原因)。这就是为什么你必须提交所有GUI更新才能在EDT上运行的原因。

Regarding the fact that you have to update your GUI with invokeLater() it is because of concurrency issues. The GUI can be modified only from one thread because Swing is not thread safe(it is worth to note that most of toolkits are not thread safe, there is a nice article that gives some ideas why). This is why you have to submit all GUI updates to run on EDT.

你可以在 Swing中并发的Sun教程。此外,如果您想了解如何以不同的方式完成此操作,您可以查看 SWT工具包。在SWT中,您必须自己管理EDT。

You can read more on concurrency in Swing and event dispatching thread in Sun tutorial on concurrency in Swing. Also, if you would like to see how this could be done in a different way you might like to check out SWT toolkit. In SWT you have to manage EDT by yourself.

这篇关于什么是事件调度线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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