欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中 MessageQueue 的 nativePollOnce

程序员文章站 2022-08-19 17:50:58
Android SDK 中的事件循环已经是一个老生常谈的问题了, 像 `Looper MessageQueue` 这几个类也是被大家研究透彻了. 但是再回头看以前自己的分析, 总感觉差点什么, 不够透彻. 心里隐隐感觉自己没有把事情完全吃透, 于是今日又回顾 Android 中的事件循环机制, 注意 ......

android sdk 中的事件循环已经是一个老生常谈的问题了, 像 handler looper messagequeue 这几个类也是被大家研究透彻了.
但是再回头看以前自己的分析, 总感觉差点什么, 不够透彻. 心里隐隐感觉自己没有把事情完全吃透, 于是今日又回顾 android 中的事件循环机制, 注意到
messagequeue 中获取下一条消息时会执行一个 native 调用 nativepollonce, 翻看 android 系统源码发现有内容.

来自高手的解释

首先, 先梭哈一把 * 上高手对这个问题()的回答原文:

short answer:

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.

explanation:

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 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). two java methods in the messagequeue class are interesting to us: message next() and boolean enqueuemessage(message, long). message next(), as its name suggest, takes and returns the next message from the queue. if the queue is empty (and there's nothing to return), the method calls native void nativepollonce(long, int) which blocks until a new message is added. at this point you might ask how does nativepollonce know when to wake up. that's a very good question. when a message is added to the queue, the framework calls the enqueuemessage method, which not only inserts the message into the queue, but also calls native static void nativewake(long), if there's need to wake up the queue. the core magic of nativepollonce and nativewake happens in the native (actually, c++) code. native messagequeue utilizes a linux system call named epoll, which allows to monitor a file descriptor for io events. nativepollonce calls epoll_wait on a certain file descriptor, whereas nativewake writes to the descriptor, which is one of the io operations, epoll_wait waits for. the kernel then takes out the epoll-waiting thread from the waiting state and the thread proceeds with handling the new message. if you're familiar with java's object.wait() and object.notify() methods, you can imagine that nativepollonce is a rough equivalent for object.wait() and nativewake for object.notify(), except they're implemented completely differently: nativepollonce uses epoll and object.wait() uses futex linux call. it's worth noticing that neither nativepollonce nor object.wait() waste cpu cycles, as when a thread enters either method, it becomes disabled for thread scheduling purposes (quoting the javadoc for the object class). however, some profilers may mistakenly recognize epoll-waiting (or even object-waiting) threads as running and consuming cpu time, which is incorrect. if those methods actually wasted cpu cycles, all idle apps would use 100% of the cpu, heating and slowing down the device.

conclusion:

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 ;)


translate.google 一下