简体   繁体   English

事件队列和消息队列的区别

[英]Difference between event queue and message queue

I was just seeing the documentation of three methods which can be used to execute a piece of code in the UI thread while we are working in a worker thread.我刚刚看到了三种方法的文档,当我们在工作线程中工作时,它们可用于在 UI 线程中执行一段代码。 The methods are:方法是:

  1. public final void runOnUIThread(Runnable action) - Runs the specified action on the UI thread. public final void runOnUIThread(Runnable action) - 在 UI 线程上运行指定的操作。 If the current thread is the UI thread, then the action is executed immediately.如果当前线程是 UI 线程,则立即执行操作。 If the current thread is not the UI thread, the action is posted to the event queue of the UI thread如果当前线程不是UI线程,则将动作发布到UI线程的事件队列中

  2. public boolean post(Runnable action) - Causes the Runnable to be added to the message queue. public boolean post(Runnable action) - 将 Runnable 添加到消息队列中。 The runnable will be run on the user interface thread. runnable 将在用户界面线程上运行。

  3. public boolean postDelayed(Runnable action, long delayMillis) - Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. public boolean postDelayed(Runnable action, long delayMillis) - 将 Runnable 添加到消息队列中,在指定的时间后运行。 The runnable will be run on the user interface thread. runnable 将在用户界面线程上运行。

The first one posts the Runnable to the event queue of the UI thread, while the other two add the Runnable to the message queue.第一个将 Runnable 发布到 UI 线程的事件队列,而其他两个将 Runnable 添加到消息队列。 Please tell me the difference between the two?请告诉我两者的区别?

My web search tell me that an event queue is simply a queue of events waiting to be executed by the thread.我的网络搜索告诉我,事件队列只是等待线程执行的事件队列。 I am not clear about the message queue.我不清楚消息队列。 MessageQueue is some class as well, is this related to that? MessageQueue 也是一些类,这与那个有关吗?

Thank you in advance.先感谢您。

I think the two are synonymous.我认为两者是同义词。 Events are indicated to the system using messages.使用消息向系统指示事件。

The real difference between the two methods is that one appends it to the queue immediately while the other delays it by the specified amount.这两种方法的真正区别在于,一种方法是立即将其添加到队列中,而另一种方法则将其延迟指定的数量。

EDIT: More on messages编辑:有关消息的更多信息

Messages are a way of communication between independent threads.消息是独立线程之间的一种通信方式。 In a way, it's a lot like the communication that takes place when you pull up a website in your browser: You send a message to the server detailing what exactly it is you want (GET www.stackoverflow.com, I will accept the following character encoding, do not track me, blablabla), which makes the server as the recipient of the message do something (retrieve content from the database, render the page, etc) and communicate the results of this back to you via a message.在某种程度上,这很像在浏览器中打开网站时发生的通信:您向服务器发送一条消息,详细说明您想要什么(获取 www.stackoverflow.com,我将接受以下内容字符编码,不要跟踪我,blablabla),这使得作为消息接收者的服务器做一些事情(从数据库中检索内容,呈现页面等)并通过消息将结果传达给您。

How it works is like this: The thread has a Looper attached to it.它的工作原理是这样的:该线程附有一个Looper All it does is run forever in a continuous loop, on each iteration checking whether there are any messages in its message queue.它所做的只是在一个连续循环中永远运行,在每次迭代时检查其消息队列中是否有任何消息。 If there aren't, it goes to the next cycle.如果没有,则进入下一个循环。 If there are, it retrieves the first message to deal with it.如果有,它会检索第一条消息来处理它。

However, the looper itself doesn't know what any of the messages mean - it's just there for looping.然而,looper 本身并不知道任何消息的含义——它只是用于循环。 Neither does the thread, which just provides the infrastructure for the looper to run in. What the looper does know, however, is who to go to for handling the message: One of its Handler s.线程也没有,它只是为循环程序提供运行的基础设施。但是,循环程序确实知道该去找谁来处理消息:它的Handler It passes the message to the handler, which can now go and do whatever it is that it needs to do to handle the message.它将消息传递给处理程序,处理程序现在可以执行任何处理消息所需的操作。

A message queue and an event queue are very similar design patterns with one notable difference.消息队列和事件队列是非常相似的设计模式,但有一个显着区别。

First off let's review the similarities.首先让我们回顾一下相似之处。 Both are asynchronous.两者都是异步的。 They store notifications in FIFO order.它们以 FIFO 顺序存储通知。 Sending a notification enqueues the event/message and returns.发送通知将事件/消息加入队列并返回。

Later on the EventManager / MessageManager will dispatch all those Event s/ Message s to the recipient objects.稍后EventManager / MessageManager会将所有这些Event s / Message s 分派给接收者对象。 The difference lies in that with MessageQueue s it's typical that the sender requires a response.不同之处在于,对于MessageQueue s,发送者通常需要响应。 With an EventQueue this is not necessary.使用EventQueue这不是必需的。

So message managing gives more control to the sender of the message.因此,消息管理为消息的发送者提供了更多的控制权。 With an event queue all the sender can do is throw a request in the queue and hope for the best.有了事件队列,发送者所能做的就是在队列中抛出一个请求并希望最好。 That additional control provided by a MessageQueue comes with a small complexity penalty. MessageQueue提供的额外控制带来了小的复杂性损失。

Choose the simplest data structure that you need for the job.选择工作所需的最简单的数据结构。

Just to make things clear : UI thread and user interface thread are the same thread and the event queue and message queue is the same queue.澄清一下:UI 线程和用户界面线程是同一个线程,事件队列和消息队列是同一个队列。

The common point between runOnUIThread and post is that both cause the Runnable to be executed on the UI thread. runOnUIThreadpost之间的runOnUIThread是两者都会导致Runnable在 UI 线程上执行。

The difference between the two, is that runOnUIThread runs the Runnable immediately when it is called from the UI Thread while for post , a message is always posted, causing it to be run after other messages.两者之间的区别在于runOnUIThread在从 UI 线程调用时立即运行Runnable而对于post ,总是发布一条消息,导致它在其他消息之后运行。

The simple answer to "which one to use" would be to use post if you don't know because if runOnUIThread is called with a Runnable that does runOnUIThread(this) , it will lock the UI thread and lead to a stack overflow.简单的答案为“使用哪一个”将使用post ,如果你不知道,因为如果runOnUIThread是带一个Runnable ,做runOnUIThread(this) ,将UI线程和铅锁定到堆栈溢出。

Source for the answer : code from Activity source code found in this page :答案来源:来自本页中的活动源代码的代码:

public final void runOnUiThread(Runnable action) {
  if (Thread.currentThread() != mUiThread) {
    mHandler.post(action);
  } else {
    action.run();
  }
}

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

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