提到Android里的消息机制,便会提到Message、Handler、Looper、MessageQueue这四个类,我先简单介绍以下这4个类

之间的爱恨情仇。

Message

消息的封装类,里边存储了消息的详细信息,以及要传递的数据

Handler

主要用在消息的发送上,有即时消息,有延迟消息,内部还提供了享元模式封装了消息对象池,能够有效的减少重复对象的创建,留更多的内存做其他的事,

Looper

这个类内部持有一个MessageQueue对象,当创建Looper的时候,同时也会创建一个MessageQueue,然后Looper的主要工作就不断的轮训MessageQueue,轮到天荒地老的那种

MessageQueue

内部持有一个Message对象,采用单项链表的形式来维护消息列队。并且提供了入队,出队的基础操作

举个现实中的栗子,Message就相当于包装好的快递盒子,Handler就相当于传送带,MessageQueue就相当于快递车,Looper就相当于快递员,联想一下,来个快递盒子,biu丢到传送带上,传送带很智能,直接传送到快递三轮车里,然后快递小哥送一波~,日夜交替,不分昼夜的工作,好家伙,007工作制

消息机制的初始化

好,我们把这4个家伙从头到位分析一遍,要想使用Android的消息,首先要创建Looper对象,Android系统已经帮我们在UI线程内创建好了一个,我们可以看一下

public final class ActivityThread extends ClientTransactionHandler {
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq); if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
} if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
} // End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");
}
}

ActivityThread这个类大家应该不陌生吧,没错,他就是我们App的主线程管理类,我们看到他调用了 prepareMainLooper 来初始化,然后 loop,天荒地老的那种loop,这个loop,我们最后聊

我们看一下Looper内部提供的 prepareMainLooper 实现

public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
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));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}

上边涉及到了3个方法,我都贴出来了,首先 quitAllowed 这个参数代表该Looper是否可以退出,我们主线程内的Looper是不允许退出的,所以封装了 prepareMainLooper 方法和 prepare 方法已做区分,我们项目中平时用的都是 prepare 方法,因为是子线程,所以允许退出Looper,大家在子线程内用完记得调用quit哦~

这里我们看Looper内部是通过ThreadLocal维护的Looper对象,也就是说每个线程都是相互独立的。而且Looper做了限制,每个线程内部只能存在一个Looper对象,等同于每个线程内只能有一个MessageQueue

最后在Looper的构造方法内,创建了一个MessageQueue对象,整个Looper的初始化就结束了

创建消息

我们准备好了Looper和MessageQueue后,就可以创建消息啦,接下来我们创建一个消息吧

//直接new对象,不推荐的方式
Message msg = new Message();
//推荐:内部是一个复用对象池
Message message = handler.obtainMessage();
message.what = 1;
message.obj = "hello world";

发送消息(入队)

我们发送消息的时候,都是会借助Handler的sendMessage就可以把消息发送到列队里了,我们往下看是如何完成的入队操作吧,首先我们平时都是创建一个Handler,然后调用sendMessage就可以了

Handler handler = new Handler();
handler.sendMessage(message);

我们先看一下Handler的构造方法

public Handler() {
this(null, false);
}
public Handler(@Nullable Callback callback, boolean async) {
//FIND_POTENTIAL_LEAKS一直都是false,所以不用关心这个逻辑
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
//得到当前线程下的Looper对象
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
//从Loopper内部获取一个列队
mQueue = mLooper.mQueue;
// 回调对象,我们平时写的时候,一般都是用类集成的方式重写 handleMessage 方法
mCallback = callback;
//标示当前Handler是否支持异步消息
mAsynchronous = async;
}

其实构造方法很简单呐,就是获取Looper对象,然后初始化列队和回调对象就完事了,我们继续看sendMessage然后看消息的入队吧

public final boolean sendMessage(@NonNull Message msg) {
return sendMessageDelayed(msg, 0);
}
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(@NonNull 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);
}

通过内部的重载方法,一直调用到sendMessageAtTime方法,在这里得到Handler内部的MessageQueue对象,然后调用了 enqueueMessage 方法准备入队

private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid(); if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}

这里调用了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放到链表最后
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;
}

通过这个方法,我们了解到MessageQueue是通过Message的单链结构存储的,然后每次入队的时候,都会

通过这个enqueueMessage方法向链表的最末尾添加数据。

最后我们聊一下Looper下的loop方法吧

接下来我们看一下

public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity(); // Allow overriding a threshold with a system prop. e.g.
// adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
final int thresholdOverride =
SystemProperties.getInt("log.looper."
+ Process.myUid() + "."
+ Thread.currentThread().getName()
+ ".slow", 0); boolean slowDeliveryDetected = false; for (;;) {
//queue的next会阻塞
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
} // This must be in a local variable, in case a UI event sets the logger
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
// Make sure the observer won't change while processing a transaction.
final Observer observer = sObserver; final long traceTag = me.mTraceTag;
long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
if (thresholdOverride > 0) {
slowDispatchThresholdMs = thresholdOverride;
slowDeliveryThresholdMs = thresholdOverride;
}
final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
final boolean logSlowDispatch = (slowDispatchThresholdMs > 0); final boolean needStartTime = logSlowDelivery || logSlowDispatch;
final boolean needEndTime = logSlowDispatch; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
} final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
final long dispatchEnd;
Object token = null;
if (observer != null) {
token = observer.messageDispatchStarting();
}
long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
try {
//派发消息,执行回调handleMessage
msg.target.dispatchMessage(msg);
if (observer != null) {
observer.messageDispatched(token, msg);
}
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} catch (Exception exception) {
if (observer != null) {
observer.dispatchingThrewException(token, msg, exception);
}
throw exception;
} finally {
ThreadLocalWorkSource.restore(origWorkSource);
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logSlowDelivery) {
if (slowDeliveryDetected) {
if ((dispatchStart - msg.when) <= 10) {
Slog.w(TAG, "Drained");
slowDeliveryDetected = false;
}
} else {
if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
msg)) {
// Once we write a slow delivery log, suppress until the queue drains.
slowDeliveryDetected = true;
}
}
}
if (logSlowDispatch) {
showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
} if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
} // Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
} msg.recycleUnchecked();
}
}

Looper内的loop方法别看这么多,大多数都是日志相关的处理。其实他就两件事

第一件事就是从列队中通过next取出Message对象

第二件事就是通过Message对象上绑定的target对象dispatchMessage方法,来分发消息

我们接下来看一下dispatchMessage方法,然后在看MessageQueue的next

public void dispatchMessage(@NonNull Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}

灰常简单,判断CallBack对象。然后调用handleMessage就完事了,我们的Activity就收到数据了。

接下来我们看看MessageQueue的next是怎么获取列队内的消息的把。

Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
} int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
//没有消息的时候,或者有延迟消息的时候会进行睡眠
nativePollOnce(ptr, nextPollTimeoutMillis); synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
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());
}
if (msg != null) {
//当前时间小于消息内记录的时间,然后计算一个睡眠时间,跳出循环执行睡眠
if (now < msg.when) {
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
} // Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
} // If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
} if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
} // Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
} if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
} // Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0; // While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}

首先MessageQueue的消息是用单链表的形式存储,然后next函数做的事情就是死循环获取消息,

在获取消息的时候判断一下消息是否符合执行时间,如果不符合执行时间,就进入睡眠状态等待消息。

如果符合执行时间就直接返回Message给Looper进行分发,如果Message链表都为空。则睡眠时间是-1

代表无休止的睡眠。在无休止睡眠的状态下,enqueueMessagenativeWake方法,会进行一次唤醒,唤醒后next函数继续执行,判断返回消息给Looper执行消息分发

Android Handler MessageQueue Looper 消息机制原理的更多相关文章

  1. [转]Handler MessageQueue Looper消息循环原理分析

    Handler MessageQueue Looper消息循环原理分析   Handler概述 Handler在Android开发中非常重要,最常见的使用场景就是在子线程需要更新UI,用Handler ...

  2. Android Handler 消息机制原理解析

    前言 做过 Android 开发的童鞋都知道,不能在非主线程修改 UI 控件,因为 Android 规定只能在主线程中访问 UI ,如果在子线程中访问 UI ,那么程序就会抛出异常 android.v ...

  3. 深入理解 Android 消息机制原理

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文讲述的是Android的消息机制原理,从Java到Native代码进行了梳理,并结合其中使用到的Epoll模型予以介 ...

  4. 图文详解 Android Binder跨进程通信机制 原理

    图文详解 Android Binder跨进程通信机制 原理 目录 目录 1. Binder到底是什么? 中文即 粘合剂,意思为粘合了两个不同的进程 网上有很多对Binder的定义,但都说不清楚:Bin ...

  5. Handler 、 Looper 、Message异步消息处理线程机制( hander消息机制原理)

    Handler . Looper .Message 这三者都与Android异步消息处理线程相关的概念. 那么什么叫异步消息处理线程呢? 异步消息处理线程启动后会进入一个无限的循环体之中,每循环一次, ...

  6. Android Handler类 发送消息-post()和postDelay(), Looper讲解

    https://blog.csdn.net/weixin_41101173/article/details/79701832 首先,post和postDelay都是Handler的方法,用以在子线程中 ...

  7. hander消息机制原理(looper轮询监听机制)

    基本原理 线程中调用Handler.sendMsg()方法(参数是Message对象),将需要Main线程处理的事件 添加到Main线程的MessageQueue中,Main线程通过MainLoope ...

  8. Android学习笔记之消息机制

    Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程.   1.为什么要使用Handler? Android规定访问UI只 ...

  9. android开发系列之消息机制

    最近接触到一个比较有挑战性的项目,我发现里面使用大量的消息机制,现在这篇博客我想具体分析一下:android里面的消息到底是什么东西,消息机制到底有什么好处呢? 其实说到android消息机制,我们可 ...

随机推荐

  1. 兄弟们,我打算抠100个网站JS加密代码召唤,一个也跑不掉,这次轮到小虎牙

    摘要:友情提示:在博客园更新比较慢,有兴趣的关注知识图谱与大数据公众号吧.本次研究虎牙登录密码加密JS代码,难度不大,依然建议各位读者参考文章独自完成,实在抠不出来再参考这里的完整代码:从今天开始种树 ...

  2. python sqlite3简单操作

    python sqlite3简单操作(原创)import sqlite3class CsqliteTable: def __init__(self): pass def linkSqlite3(sel ...

  3. java开发,入职半年,对未来迷茫,如何发展?

    分享-更多精彩图片尽在大师助手 个人建议,在JAVA方面,先学好JAVA SE.不管如何,基础才是上层建筑的一切.推荐去看jdk源码,推荐1.6或者1.7版本.因为1.8版本的源码中会有很多lambd ...

  4. XMLHttpRequest 简单封装

    当开发简单页面的时候,不需要引入任何js库,这时需要封装一个用到 XMLHttpRequest 对象的好用的接口请求. simple 封装如下 ajaxRequest({ url: '', metho ...

  5. gson 处理null

    1.定义null处理类 class StringConverter : JsonSerializer<String?>, JsonDeserializer<String?> { ...

  6. 【jmespath】—2. 进阶 List and Slice Projections

    Projections翻译的话说实话我也不知道翻成什么比较准确,姑且就叫"投影"吧,不过带来的功能确实非常的强大.好用. 首先,我先说下我的理解: 通配符 * 通配符生成的是一个l ...

  7. Codeforces 1321D Navigation System

    题意 有个人要从\(s\)走到\(t\),经过的路径给定.导航系统每次会显示当前节点到\(t\)的最短路,有多条就显示其中之一.这个人如果按照导航走,那么啥都没变.如果没有按导航走导航就会重新导航.问 ...

  8. php中的加密解密模块-mcrypt

    <?php /* 打开加密算法和模式 */ $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); /* 创建初始向量,并且检测密钥长度 ...

  9. ASP.NET Uploadify 上传文件过大 报错(http error)借鉴,以防忘记

    Uploadify上传文件原来很早之前用过,没发现什么问题.今天再使用过程中,当文件大于30M的时候就会报错404.查看错误消息提示配置最大上传太小了.需要修改. 记得原来配置上传文件大小在这里:&l ...

  10. 小程序开发-使用Loading和Toast提示框

    小程序提示框 Loading提示框使用方式 1. 在wxml中作为组件使用 <loading hidden="{{hidden}}"> 加载中... </load ...