Adnroid 源码学习笔记:Handler 线程间通讯
常见的使用Handler线程间通讯:
主线程:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
...
}
};
子线程:
Message message = new Message();
message.arg1 = 1;
Bundle bundle = new Bundle();
bundle.putString("test", "test");
message.setData(bundle);
handler.sendMessage(message);
这类操作一般用于在子线程更新UI。在主线程创建一个handler,重写handlermessage方法,然后在子线程里发送消息,主线程里就会接受到消息。这就是简单的线程间通讯。如果在子线程创建handler对象则会报错。根据Log提示,子线程创建handler需要调用Looper.prepare() (在main函数中已经调用了Looper.prepareMainLooper(),该方法内会调起Looper.prepare()),Looper.loop()方法 。但是即使子线程调用Looper.prepare()创建Looper对象,这个Looper也是子线程的,不可以用于更新UI操作。那到底Handler、Looper这几个类之间是如何工作的呢?我们从源头看起,以下是Looper类的prepare()方法:
public final class Looper {
...
final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); //threadLocal是线程内部的数据存储类,该类存储了线程的所有数据信息。
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed)); //创建一个Looper(Looper的构造器里也创建了一个MessageQueue,),将Looper与线程关联起来
}
public static @Nullable Looper myLooper() { //下面会看到的,设置Handler类里的Looper时会调用该方法
return sThreadLocal.get(); //获得Looper对象
}
...
}
当handler传输message时,不论是调用sendMessage(Message msg)还是sendMessageDelayed(),最后都会指向sendMessageAtTime()方法:
public class Handler {
...
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
...
}
mQueue即消息队列,用于将收到的消息以队列形式排列,提供出队和入队方法,该变量是Looper的成员变量,在Handler创建时赋值给handler
public class Handler {
...
final Looper mLooper;
final MessageQueue mQueue;
mLooper = Looper.myLooper(); //创建Handler前调用Looper.prepare()时定义并设置了Looper,这里调用Looper.myLooper()来获得该Looper
mQueue = mLooper.mQueue;
...
}
上面调用的enqueueMessage(queue, msg, uptimeMillis)方法作用是消息入队
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this; //把handler本身赋值给要入队的消息,用来待会儿出队使用
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
可以看到最后是由消息队列queue调用自身MessageQueue类的入队方法enqueueMessage()
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
mMessages是MessageQueue类的一个成员变量,用以记录排在最前面的消息,msg是我们传入的message,msg.next是Message类的成员变量,可以理解成下一条消息。入列方法重点看这几句:
Message p = mMessages;
msg.next = p; //把mMessages赋值给新入队的这条消息的next
mMessages = msg; //把新入队的消息赋值给mMessages
就像排队一样,msg是来插队的,排第一的mMessages自愿排到msg的后面,并让msg站到自己原来的位置上,这样就完成的msg的入队操作,整个消息入队操作是按照时间来排序的。至于出队操作,就在一开始所提到的ActivityTread中的main方法里调用的Looper.loop()方法里:
public static void loop() {
...
for (;;) {
...
Message msg = queue.next(); //获取下一条消息
...
msg.target.dispatchMessage(msg); //传递消息
...
msg.recycleUnchecked(); //清空状态,循环往复
}
}
...
}
提炼出来就是在loop方法里一直死循环,从MessageQueue消息队列里使用next()方法获得下一条消息,next方法简单看就是:
Message msg = mMessages;
mMessages = msg.next;
msg.next = null;
return msg;
这就是简单的解释消息出列,把排第一的消息作为方法的返回值,然后让排第二的排到第一去。获得消息后使用msg.target(上面入队时赋值的handler)来传递消息:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg); //如果有callback参数则调用处理回调的方法
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg); //将消息作为参数传递出去
}
}
至此,handler传递消息的整个流程走完。另外还有一个我们经常用到handler的方法post:
public final boolean post(Runnable r) {
return sendMessageDelayed(getPostMessage(r), 0);
}
private static Message getPostMessage(Runnable r) { //将runnable变成message自身的callback变量
Message m = Message.obtain();
m.callback = r;
return m;
}
可以看到,post的runnable参数经过getPostMessage()方法最后被赋值给要传递下去的消息的callback这个变量,等到消息出列时,如果消息带有callback参数则调用处理回调的方法handleCallback(msg)
private static void handleCallback(Message message) {
message.callback.run();
}
可以看到,不论是从sendMessage里发出的消息,还是在post传递的runnable里执行的代码,最后都是殊途同归,都是在UI线程运行的。最后总结一下吧,线程间通讯原理大概就是:
- Looper.prepare()创建Looper和MessageQueue,并与所在线程关联
- Looper.loop()通过一个for死循环不断对MessageQueue进行轮询
- 创建handler时,会把Looper和MessageQueue赋值给handler,将三者关联起来。当handler调用sendMessage传递消息,消息会被发送到Looper的消息队列MessageQueue里
- 一旦loop()方法接收到消息,则将消息通过该消息携带的handler(msg.target)的handleMessage方法处理
Adnroid 源码学习笔记:Handler 线程间通讯的更多相关文章
- JUC源码学习笔记5——线程池,FutureTask,Executor框架源码解析
JUC源码学习笔记5--线程池,FutureTask,Executor框架源码解析 源码基于JDK8 参考了美团技术博客 https://tech.meituan.com/2020/04/02/jav ...
- zeromq源码分析笔记之线程间收发命令(2)
在zeromq源码分析笔记之架构说到了zmq的整体架构,可以看到线程间通信包括两类,一类是用于收发命令,告知对象该调用什么方法去做什么事情,命令的结构由command_t结构体确定:另一类是socke ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- Hadoop源码学习笔记(3) ——初览DataNode及学习线程
Hadoop源码学习笔记(3) ——初览DataNode及学习线程 进入了main函数,我们走出了第一步,接下来看看再怎么走: public class DataNode extends Config ...
- Hadoop源码学习笔记(5) ——回顾DataNode和NameNode的类结构
Hadoop源码学习笔记(5) ——回顾DataNode和NameNode的类结构 之前我们简要的看过了DataNode的main函数以及整个类的大至,现在结合前面我们研究的线程和RPC,则可以进一步 ...
- Hadoop源码学习笔记(4) ——Socket到RPC调用
Hadoop源码学习笔记(4) ——Socket到RPC调用 Hadoop是一个分布式程序,分布在多台机器上运行,事必会涉及到网络编程.那这里如何让网络编程变得简单.透明的呢? 网络编程中,首先我们要 ...
- JUC源码学习笔记2——AQS共享和Semaphore,CountDownLatch
本文主要讲述AQS的共享模式,共享和独占具有类似的套路,所以如果你不清楚AQS的独占的话,可以看我的<JUC源码学习笔记1> 主要参考内容有<Java并发编程的艺术>,< ...
- JUC源码学习笔记4——原子类,CAS,Volatile内存屏障,缓存伪共享与UnSafe相关方法
JUC源码学习笔记4--原子类,CAS,Volatile内存屏障,缓存伪共享与UnSafe相关方法 volatile的原理和内存屏障参考<Java并发编程的艺术> 原子类源码基于JDK8 ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
随机推荐
- Android source code compile error: “Try increasing heap size with java option '-Xmx<size>'”
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g" ./pr ...
- Linux的硬盘挂载
一·前言 我朋友买了一个香港的服务器,可用总容量为60G,实际只有15.4G,剩下的容量需要硬盘挂载.他尝试无果,向我求助.我帮他解决了问题,想回顾一下整理写此随笔. 二·运行环境 Linux系统版本 ...
- LeetCode 041 First Missing Positive
题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...
- java顺序、选择、循环结构
一.顺序结构 二.选择结构 1.if都执行 2.if else if else 条件满足才执行 3.选择结构switch 一个case后有多条语句要加花括号 多个case的值不能相同 case中要加b ...
- MongoDB 分片集群配置
本文测试环境为 CentOS 7 和 MongoDB 最新版 (4.0.12) 使用 root 操作 (实际操作中使用非 root 账户启动报错) 零.服务器分配 服务器 102 服务器 103 服务 ...
- Pentaho Report Designer 入门教程(一)
PentahoReport Designer 入门教程 采用Pentaho Report Designer5.1版本,也是最新的版本. 一. 安装和介绍 介绍部分内容略,首先安装jdk,并 ...
- jquery 执行a 标签 点击事件 跳转href 路径
<a href="./export.pdf" id="pdfdown" download="文件名.pdf">下载</a& ...
- 【Codeforces 1097F】Alex and a TV Show(bitset & 莫比乌斯反演)
Description 你需要维护 \(n\) 个可重集,并执行 \(m\) 次操作: 1 x v:\(X\leftarrow \{v\}\): 2 x y z:\(X\leftarrow Y \cu ...
- 膜 zhouakngyang 宝典
持续更新! 注意:本文无 F12. about 周老师:怎么这么强! ZAKY 打 CF 大图:zaky cgr rk1 大图:zaky 传奇 \(1\) ZAKY 打 ATC ZAKY 切题
- 调用windows系统下的cmd命令窗口处理文件
从后缀名为grib2的文件中查询相关的信息,并将查出来的信息保存起来. 主要是学习java中调用windows下的cmd平台,并进行执行相关的命令. package com.wis.wgrib2; i ...