异步消息机制源码分析
来源:互联网 发布:抢演唱会门票软件 编辑:程序博客网 时间:2024/06/05 21:17
我们进入Handler的源码我们可以看到
public Handler(Callback callback) { this(callback, false); } public Handler(Callback callback, boolean async) { 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()); } } mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; }
Looper.myLooper();拿到当前线程的Looper实例
mLooper.mQueue;拿到他的消息队列
并进行一个判空
如果Looper为空则会报一个异常
在来看看 Looper.myLooper();进入looper源码看到
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
返回的就是一个looper
但是主线程他已经帮我们调用了Looper.prepare()
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(); }
可以看到在这里会在本线程冲保存一个Looper实例和他的消息队列
我们在看看handler的发送消息都是通过sendMessage();进入源码查看我们可以看到最终
调用的是
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
msg.target = this;
他调用把消息队列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(); for (;;) { 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); } final long traceTag = me.mTraceTag; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); } try { msg.target.dispatchMessage(msg); } finally { if (traceTag != 0) { Trace.traceEnd(traceTag); } } 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(); } }
他主要就是创一个消息循环,不断的从消息队列中拿取消息
Message msg = queue.next();
最后通过msg.target.dispatchMessage(msg);发送消息,
开始说道msg.target其实就是Handler
再来看看dispatchMessage(msg);
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
最终调用的就是 handleMessage(msg);来处理消息
总结:
1.创建Handler的时候会拿到当前线程的Looper实例,和他的消息队列构成关联,形成一个循环通过queue.enqueueMessage(msg, uptimeMillis);保存消息到消息队列中
2.主线程调用Looper.prepare();在本线程中保存一个Looper实例和他的消息队列。
3.Looper.loop()拿到当前线程的Looper实例和他的消息队列,创建消息循环体。
4.通过Message msg = queue.next(); 不断的从消息队列中取出消息通过 。msg.target.dispatchMessage(msg);发送消息 。
5.最终调用handleMessage(msg);来处理消息形成一个循环。
- 异步消息机制源码分析
- 异步消息机制源码分析
- Android异步消息机制及源码分析
- 源码分析 -- 异步消息处理机制
- Android异步消息机制及源码分析
- Android源码分析-异步消息机制
- Android异步消息处理机制详解及源码分析
- Android异步消息处理机制详解及源码分析
- Android异步消息处理机制(二):源码分析
- Android异步消息处理机制详解及源码分析
- Looper、Message、MessageQueue、Handler异步消息处理机制源码分析
- Android异步消息处理机制详解及源码分析 Handler
- Android异步消息处理机制的源码分析
- Handler异步消息处理机制的源码分析
- 异步消息处理机制之Handler源码分析篇
- Android异步消息处理机制详解及源码分析
- Android异步消息处理机制详解及源码分析
- Android异步消息处理机制详解及源码分析
- js html 轮播图 支持移动端左右滑动 pc也能用
- Java异常的深入研究与分析
- 临界区
- Git教程(二)
- ubuntu搜狗输入法崩溃
- 异步消息机制源码分析
- python中str与list互换
- No module named win32api问题解决
- CNN-卷积反卷积
- gitlab备份-源码安装
- mysql COALESCE的作用
- 大白话讲解遗传算法
- 安装ansible时,报fatal error: Python.h: No such file or directory
- android如何客制化紧急号码?