Android Handler源码深入探究
作者:niuyongzhi 发布时间:2023-02-17 11:54:53
1.android 消息循环有4个重要的类Handler、Message、Looper、MessageQueue
handler 用来发送、处理消息。
Message 是消息的载体。
MessageQueue 是一个消息队列,既然是队列,就有入队、出队的处理。
Looper 创建一个消息循环。不断的从MessageQueue中读取消息、并分发给相应的Handler进行处理。
2.我们都知道main函数是Java程序的入口,android程序也不例外。
android App的唯一入口就是ActivityThread中的 main函数。 这个函数是由Zygote创建app进程后 通过反射的方式调用的。
当一个App启动时,会先执行这个main方法,在ActivityThread,main方法中,
public static void main(String[] args) {
//创建一个消息循环
Looper.prepareMainLooper();
//创建ActivityThread对象
ActivityThread thread = new ActivityThread();
//创建Application、启动MainActivity
thread.attach(false, startSeq);
//使消息循环奔跑起来
Looper.loop();
//抛了一个异常 主线程的Looper 意外退出了,
//所以loop中的for循环要阻塞在这里,一旦main函数执行完毕,进程也就退出了。
//并且一直要提取消息,处理消息。
throw new RuntimeException("Main thread loop unexpectedly exited");
}
3. 首先看Looper是如何创建的。
Looper.prepareMainLooper();
public static void prepareMainLooper() {
prepare(false);
//同步方法保证一个sMainLooper 只被赋值一次。
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
//创建一个Looper对象,并保存在了 sThreadLocal中。关于ThreadLocal,也是很重要的一个知识点。
private static void prepare(boolean quitAllowed) {
//一个线程只能有一个Looper,
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
//在Looper中创建了MessageQueue
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
至此,创建的代码执行完毕。
总结一句话就是,app启动时,会创建Looper,并且保证一个线程只能创建一个Looper。
创建Looper的同时,也创建的消息队列 MessageQueue。这些都是消息循环的准备工作。
通过Looper.loop,这个消息循环就跑起来了。
Looper.loop();
/**
* Run the message queue in this thread.
*/
public static void loop() {
for (; ; ) {
//从消息循环中提取消息。消息时会阻塞在这里。
Message msg = queue.next(); // might block
//target-->Handler.Msg持有handler的引用。
msg.target.dispatchMessage(msg);
}
}
4.在MessageQueue中enqueueMessage()插入消息、next()提取消息方法。
插入消息
//Message.obtain()从Message消息缓存池内获得一个消息。
handler.sendMessage(Message.obtain());
public final boolean sendMessage(Message msg){
return sendMessageDelayed(msg, 0);
}
//我们发送的延迟消息,写入到消息循环中都是一个时间戳,当前时间+延迟时间,未来某个时间。
public final boolean sendMessageDelayed(Message msg, long delayMillis){
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
return enqueueMessage(queue, msg, uptimeMillis);
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
//this==handler,在这里给message.target赋值了handler。
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
//在MessageQueue中用一个单线链表来保存消息。
//在这个消息的单链表中,是按消息执行的时间先后,从小到大排序的。
//mMessages 是这个单链表的第一个消息。
boolean enqueueMessage(Message msg, long when) {
//通过sendMessage并不能发送handler==null的消息。
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
synchronized (this) {
msg.markInUse();
msg.when = when;
//将链表中第一个消息赋值给p
Message p = mMessages;
boolean needWake;
//如果p==null说明消息列表中没有要被执行的消息。
//如果when==0说明新新添加的消息要被马上执行,所以要排在列表的头部
//如果when<p.when,说明新添加的消息,比消息队列第一个消息要先执行,所以也要放在头部
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
//将原来的头部消息赋值给新消息的next
msg.next = p;
//将新加的消息赋值给mMessage。因为mMessages这个变量用来保存消息列表的第一个消息。
mMessages = msg;
} else {//如果when>=p.when,则需要遍历消息队列,将新添加的消息插入到队列中间,
Message prev;
for (;;) {
prev = p;//把当前的赋值给前一个
p = p.next;//把下一个赋值给当前的
//如果p==null说明已经遍历到了链表末尾。
//如果新增的消息时间小于了p的when。那么这个消息应该插入到prev之后,p之前。
if (p == null || when < p.when) {
break;
}
}
//新增消息在p之前,prev之后
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
}
return true;
}
提取消息:
Message next() {
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
//开始休眠,nextPollTimeoutMillis下次被唤醒的时间
//如果是-1则一直休眠,直到有新的消息再唤醒消息队列。
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
//开始遍历消息队列,返回找到的消息。
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
//消息队列,头部消息
//如果msg!=null,但是msg.target==null,sendMessage中,是不允许发送handler为null的消息的。
//target==null的消息是系统发送的,先发送一个同步屏障消息,再发送直到isAsynchronous = true的异步消息。
//这样做的目的就保证了这个异步消息有更高优先级被执行,先从消息队列中提取。
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());//直到isAsynchronous = true,也就是找到了同步屏障的异步消息
}
}
if (msg != null) {
if (now < msg.when) {//当前时间小于消息执行的时间,记录一下差值
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {//如果当前时间,不小于取到消息的执行时间,则从消息队列中提取出该消息,并返回出去
// Got a message.
mBlocked = false;
if (prevMsg != null) {
//将前一个消息的下一个消息指向,当前消息的下一个。
prevMsg.next = msg.next;
} else {
//如果前一个消息是null,说明当前就是消息头,
//将消息队列头部消息指向当前提取出消息的下一个消息。
mMessages = msg.next;
}
//将找到的消息的下一个赋值null,和原来的消息队列脱离关系。
msg.next = null;
if (false) Log.v("MessageQueue", "Returning message: " + msg);
return msg;
}
} else {//如果msg==null
// No more messages.
nextPollTimeoutMillis = -1;
}
}
}
}
handler会涉及到native的代码。在native层使用的epoll机制,这个后面在深入分享。
这里涉及到了一个消息屏障的概念,有机会单独写文章来分享。
//同步屏障消息
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
//发送一个同步屏障消息
mTraversalBarrier = mHandler.getLooper().postSyncBarrier();
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
}
}
5. 至此,整个消息循环大体的流程已经完成。但是关于handler的面试题很多。
比如为啥handler会导致Activity内存泄漏?如何解决?
内存泄漏的本质就是长声明周期对象持有短声明周期对象的引用,导致短声明周期对象,不再使用但内存却无法被回收。
我们知道handler作为Activity的内部类,持有外部类的引用,所以整个引用链是
Activity-->handler-->Message-->MessageQueue.
当activity退出后,如果消息为来的及处理,就有可能会导致Activity无法被GC回收,从而导致内存泄漏。
handler.post(),发送的消息执行在子线程还是主线程?
下面来看消息池。消息池也是一个单项链表,长度是50.
静态对象sPool就是消息队列的头部Message。
每次获取消息时,都会返回消息池中第一个对象。
Message.obtain()
private static Message sPool;
private static final int MAX_POOL_SIZE = 50;
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}
来源:https://blog.csdn.net/niuyongzhi/article/details/125882150
猜你喜欢
- 目录顶级程序顶级程序中的方法顶级程序中的类顶级程序的原理分析当我们用 C# 进行编码的时候,总需要写很多的模板代码,即使是最简单的 cons
- 问题描述:在windows系统下,idea中,操作terminal控制台,使用git log查看日志时,出现如下乱码为什么参考网上很多的gi
- 前言前阵子有同学反馈Flutter中的http请求无法通过fiddler抓包,作者喜欢使用Charles抓包工具,于是抽时间写了个小demo
- mybatis count()按条件查询1、sql count()函数count()函数返回匹配指定条件的行数。sql count(colu
- @Value("${xxx}")取properties时中文乱码(1)检查spring的配置文件中,properties
- 用法在java中经常会遇到需要对数据进行类型转换的场景,String类型的数据转为Int类型属于比较常见的场景,主要有两种转换方法:1. 使
- 事件缘由:一个主项目“图说美物”,另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,
- Dubbo作为国内最出名的分布式服务框架,是Java程序员必备必会的框架之一,更是中高级测试面试过程中经常会问的技术,无论你是否用过,你都必
- 本文实例讲述了C#判断一天、一年已经过了百分之多少的方法。分享给大家供大家参考。具体如下:这里写了四个函数,分别是1.判断当前时间过了今天的
- 对于分布式应用来说,最开始遇到的问题就是 session 的存储了,解决方案大致有如下几种使用 spring-session 它可以把 se
- 概述在JAVA JDK8 List分组的实现和用法一文中介绍了JDK 8如何对list进行分组,但是没有提到如何在分组后,获取每个分组的第一
- springBoot Junit测试用例出现@Autowired不生效前提条件:1,测试类上面添加支持的注解就能取到spring中的容器的实
- 前言我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是java.util.concurrent包的核心,没有volat
- 本文实例为大家分享了使用C#写出一个简单的记事本程序,供大家参考,具体内容如下编程语言: C# 编程环境: Visual Studio 20
- 前言首先,我们要讲的是JVM的垃圾回收机制,我默认准备阅读本篇的人都知道以下两点:JVM是做什么的Java堆是什么因为我们即将要讲的就是发生
- ealsticsearch只是后端提供各种api,那么怎么直观的使用它呢?elasticsearch-head将是一款专门针对于elasti
- 本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下import com.alibaba.fastjson.JS
- java 定时任务在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现。下面
- ForkJoinTask就是ForkJoinPool里面的每一个任务。他主要有两个子类:RecursiveAction和RecursiveT
- 本文基于SpringBoot 2.5.0-M2讲解Spring中Lifecycle和SmartLifecycle的作用和区别,以及如何控制S