android - 什么是android中的消息队列本机轮询? [英] android - what is message queue native poll once in android?
问题描述
我知道线程有一个消息队列,处理程序可以将runnables或消息推送到它们,但是当我使用Android Studio工具分析我的Android应用程序时,有一个奇怪的过程:
I know that threads have a message queue and handlers are able to push runnables or messages to them, but when I profile my android application using Android Studio tools, there is an strange process:
android.os.MessageQueue.nativePollOnce
android.os.MessageQueue.nativePollOnce
它使用的CPU超过所有其他过程。它是什么以及如何减少CPU花费的时间?
您可以在下面找到分析器结果。
It uses the CPU more than all other processes. What is it and how can I reduce the time that the CPU spends on that? You can find the profiler result in the below.
推荐答案
简答:
nativePollOnce
方法用于等待直到下一个消息
变为可用。如果在此调用期间花费的时间很长,则您的主(UI)线程无需执行任何实际操作,并等待下一个要处理的事件。没有必要担心这一点。
The nativePollOnce
method is used to "wait" till the next Message
becomes available. If the time spent during this call is long, your main (UI) thread has no real work to do and waits for next events to process. There's no need to worry about that.
说明:
因为 main线程负责绘制UI并处理各种事件,它的 Runnable
有一个处理所有这些事件的循环。
循环由 Looper
管理,其工作非常简单:它处理 MessageQueue
。
Because the "main" thread is responsible for drawing UI and handling various events, it's Runnable
has a loop which processes all these events.
The loop is managed by a Looper
and its job is quite straightforward: it processes all Messages in the MessageQueue
.
A 消息
被添加到队列中,例如响应输入事件,作为帧渲染回调或甚至你自己的 Handler.post
电话。有时主线程没有工作要做(即队列中没有消息),这可能发生在例如在完成渲染单帧之后(线程刚刚绘制了一帧并准备好下一帧,只需等待适当的时间)。因为消息以不可预测的方式添加到队列,所以主动 MessageQueue.next
方法(其中 nativePollOnce
被调用)检查使用CPU周期的新消息。
A Message
is added to the queue for example in response to input events, as frame rendering callback or even your own Handler.post
calls. Sometimes the main thread has no work to do (that is, no messages in the queue), which may happen e.g. just after finishing rendering single frame (the thread has just drawn one frame and is ready for the next one, just waits for a proper time). Because Messages are added to the queue in unpredictable way, the MessageQueue.next
method (in which nativePollOnce
is called) actively checks for new Messages, which uses CPU cycles.
结论:
你不要担心 nativePollOnce
。它只表示已完成所有消息的处理,并且线程等待下一个消息。好吧,这只是意味着你不会给你的主线程太多工作;)
You shouldn't worry about nativePollOnce
. It just indicates that processing of all Messages has been finished and the thread waits for the next one. Well, that simply means you don't give too much work to your main thread ;)
这篇关于android - 什么是android中的消息队列本机轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!