事件队列和消息队列之间的区别 [英] Difference between event queue and message queue

查看:1488
本文介绍了事件队列和消息队列之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是看到了三种方法可用于在UI线程执行一段code,而我们正努力在工作线程的文档。其方法为:

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. The methods are:

  1. 公众最终无效runOnUIThread(Runnable的动作) - 运行在UI线程上指定的操作。如果当前线程是UI线程,则该动作被立即执行。如果当前线程不是UI线程,该操作被张贴到UI线程的事件队列

  1. public final void runOnUIThread(Runnable action) - Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread

公共布​​尔交(Runnable的动作) - 使被添加到消息队列中的可运行。可运行将在用户界面线程中运行。

public boolean post(Runnable action) - Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.

公共布​​尔postDelayed(Runnable的作用,长期delayMillis) - 导致了Runnable要添加到消息队列,时间经过指定金额后运行。可运行将在用户界面线程中运行。

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. The runnable will be run on the user interface thread.

第一个职位可运行到UI线程的事件队列,而其他两个添加可运行到消息队列。请告诉我两者之间的区别是什么?

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. Please tell me the difference between the two?

我的网络搜索告诉我,事件队列仅仅是事件等待的线程执行队列。我不清楚有关消息队列。 MessageQueue一些类为好,这是相关的?

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?

感谢你在前进。

推荐答案

我认为两者是同义的。事件指示给使用信息系统。

I think the two are synonymous. Events are indicated to the system using messages.

这两种方法之间的真正区别是,它追加到队列立即而另一延迟其指定的量。

The real difference between the two methods is that on appends it to the queue immediately while the other delays it by the specified amount.

编辑:更多信息

消息是独立的线程间通信的一种方式。在某种程度上,这是一个很像是发生在你拉在你的浏览器的网站通信:发送一条消息给服务器,详细说明它究竟你想要的(GET www.stackoverflow.com,我将接受以下字符编码,不跟踪我,blablabla),这使得服务器作为邮件的收件人做一些事情(从数据库中检索内容,渲染页面,等等),并通过信息沟通这回给你的结果。

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.

它是如何工作是这样的:线程有一个尺蠖连接到它。它所做的就是一直运行在一个连续的循环,在每次迭代检查是否有它的消息队列中的任何消息。如果没有,它前进到下一个周期。如果有,它检索的第一个消息来处理它。

How it works is like this: The thread has a Looper attached to it. 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. 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 Handlers. 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.

这篇关于事件队列和消息队列之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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