Handler中MessageQueue的enqueueMessage笔记
今天心血来潮,又默默的想着Handler的实现原理,毕竟面试这个可是热门的话题,虽然看了很多遍源码,但每次都是研究Loop和Handler的关系,今天看着看着进到MessageQueue的源码里,了解了下把Message入队的操作,分享给大家。
首先我们先看Message类,根据名字我们可以知道它是消息的意思,我们每次调用handler.post(Runnable)系统都会调用getPostMessage方法 转换成一个Message,它内部有个变量next,它的类型也是Message,数据结构基础好的童鞋应该会反应过来,没错Message类似于一个链表结构。
进入正题先看enqueueMessage部分源码:
boolean enqueueMessage(Message msg, long when) {
msg.when = when;
Message p = mMessages;
if (p == null || when == 0 || when < p.when) {
boolean needWake;
msg.next = p;
// New head, wake up the event queue if blocked.
mMessages = msg;
// Inserted within the middle of the queue. Usually we don't have to wake
needWake = mBlocked;
} else {
// and the message is the earliest asynchronous message in the queue.
// up the event queue unless there is a barrier at the head of the queue
p = p.next;
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
msg.next = p; // invariant: p == prev.next
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p;
prev.next = msg;
}
prev.next = msg;
也就是把插到该消息的前面,优先执行新的消息。
Handler中MessageQueue的enqueueMessage笔记相关推荐
- Handler ,MessageQueue 的Looper中epoll监听的fd
hi,同学们大家好! 这些天有学员再群里问起了Handler中有个数据监听相关问题,学员有的认为Handler数据传递是靠流传递,误认为是epoll中监听的fd进行传递的,这个其实有必要更正这个学员的 ...
- 【Android 异步操作】Handler 机制 ( MessageQueue 消息队列的阻塞机制 | Java 层机制 | native 层阻塞机制 | native 层解除阻塞机制 )
文章目录 一.MessageQueue 的 Java 层机制 二.MessageQueue 的 native 层阻塞机制 三.MessageQueue 的 native 层解除阻塞机制 三.Messa ...
- Android 线程4件套 MessageQueue Message Looper Handler之MessageQueue
看了前面Looper章节之后,也许还是云里雾里的,但至少了解到Looper中每个Looper干了什么事,其中我们MessageLooper留下了两个问题.主要问题是Looper如何从MessageQu ...
- Android 消息处理机制(Looper、Handler、MessageQueue,Message)
原文:[简书原文](http://www.jianshu.com/p/02962454adf7)> 另外本人自己开了开了个人博客 fanmingyi.cn 不定时更新[跳转到个人博客](http ...
- 了解Handler,Looper, MessageQueue,Message的工作流程
Handler的作用 异步通信,消息传递 Handler的基本用法 Handler的用法,示例1.(子线程向主线程发送消息) public class HandlerActivity extends ...
- Android Handler中的handleMessage方法和post方法之源码剖析
我们都知道,在子线程中进行UI操作(更新UI控件)包括以下四种方法: 1.Handler的handlerMessage()方法. 2.Handler的post()方法. 3.View的post()方法 ...
- 深入理解异步消息处理机制Message,handler,MessageQueue,looper
本文转自: http://www.cyqdata.com/android/article-detail-36658 本篇深入理解Android消息队列原理篇,将为您讲述Android世界中的消息机制, ...
- Android异步处理三:Handler+Looper+MessageQueue深入详解
本博文地址:http://blog.csdn.net/mylzc/article/details/6771331 转载请注明出处 Android异步处理系列文章索引 Android异步处理一:使用Th ...
- 【Android 异步操作】Handler 机制 ( MessageQueue 空闲任务 IdleHandler 机制 )
文章目录 一.MessageQueue 空闲任务 IdleHandler 机制 二.MessageQueue 中空闲任务 IdleHandler 相关源码 一.MessageQueue 空闲任务 Id ...
最新文章
- 使用python套用excel模板_集成python的excel插件模板pyaddin——使用说明
- 无法远程访问Mysql的解决方案
- P4391 [BOI2009]Radio Transmission 无线传输
- java中break,continue,標籤實現goto效果(編程思想)
- 如何在SAP Cloud Platform上进行第一个integration flow开发
- 科普帖:什么是服务网格?
- 运算均值方差_Python数据分析之NumPy(运算篇)
- c语言注释和代码互换,c语言项目—注释转换(c——c++)
- 耳挂式蓝牙耳机原理_挂耳式蓝牙耳机如何佩戴
- hadoop2.4的伪集群的搭建
- 从阿尔法元的诞生看终极算法的可能性
- 2021第四届全国大学生IT技能大赛“传智杯”AK
- 关于ADS-Matlab联合仿真ADS.RunSimulation()报错的解决方案
- 计算机图形图像处理应用教程,计算机图形图像处理应用教程
- OpenShift 4 - 用 External Secret 集成 Hashicorp Vault
- IB中文:文学作品中幽默手法的表现方式(下)
- echart自定义图例icon的形状
- 学计算机要买什么笔记本,学计算机的买什么笔记本
- 雨林木风 Ghost XP SP2 精简版 Y2.0
- 请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。