Android 中 MessageQueue 的 nativePollOnce
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 一下
上一篇: B站自动刷弹幕
下一篇: MySQL如何进行索引重建操作?
推荐阅读
-
Android App中DrawerLayout抽屉效果的菜单编写实例,drawerlayout上下抽屉_PHP教程
-
android教程之把自己的应用加入到系统分享中
-
Android中数据解析的五种方式
-
Android中再按一次退出提醒实现的两种方法
-
【JNI】C++ 中的Android log使用格式化字符串输入
-
Android中创建一个透明的进度对话框实例
-
android中判断sim卡状态和读取联系人资料的方法
-
Android自定义View中attrs.xml的实例详解
-
Android TextView 在java代码中改变字体的颜色的方法
-
Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题