Android之Handler和Loooper源码分析

1、handler在主线程和子线程互相通信(子线程和子线程的通信)简单使用

      我们使用handler,可以实现主线程和子线程之间的相互通信,然后子线程和子线程之间的通信,如果不清楚,基本用法请先参考我的这篇博客

Android之用Handler实现主线程和子线程互相通信以及子线程和子线程之间的通信  http://blog.csdn.net/u011068702/article/details/75577005

 

 

 

2、handler在主线程为什么不需要调用Looper.prepare()

我们看下Looper.java这个类,它在安卓android.os包下,我们看这个类的一开始的注释

 

  * <p>This is a typical example of the implementation of a Looper thread,* using the separation of {@link #prepare} and {@link #loop} to create an* initial Handler to communicate with the Looper.** <pre>*  class LooperThread extends Thread {*      public Handler mHandler;**      public void run() {*          Looper.prepare();**          mHandler = new Handler() {*              public void handleMessage(Message msg) {*                  // process incoming messages here*              }*          };**          Looper.loop();*      }*  }</pre>


很明显,在一个线程里面需要使用Handler之前需要Looper.prepare(),但是我们平时在主线程更新UI的时候,为什么没有看到这行代码呢?

 

我们看下ActivityThread.java这个类,它在安卓包名android.app目录下,我们知道ActivityThread.java这个类是安卓程序的入口,我们看下main函数的代码

 

    public static void main(String[] args) {SamplingProfilerIntegration.start();// CloseGuard defaults to true and can be quite spammy.  We// disable it here, but selectively enable it later (via// StrictMode) on debug builds, but using DropBox, not logs.CloseGuard.setEnabled(false);Environment.initForCurrentUser();// Set the reporter for event logging in libcoreEventLogger.setReporter(new EventLoggingReporter());Security.addProvider(new AndroidKeyStoreProvider());// Make sure TrustedCertificateStore looks in the right place for CA certificatesfinal File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());TrustedCertificateStore.setDefaultUserDirectory(configDir);Process.setArgV0("<pre-initialized>");Looper.prepareMainLooper();ActivityThread thread = new ActivityThread();thread.attach(false);if (sMainThreadHandler == null) {sMainThreadHandler = thread.getHandler();}if (false) {Looper.myLooper().setMessageLogging(newLogPrinter(Log.DEBUG, "ActivityThread"));}Looper.loop();throw new RuntimeException("Main thread loop unexpectedly exited");}


我们可以看到有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();}}


然后看到了prepare(false)函数,我们再点击这个函数

 

 

    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));}


我们可以看到这里就调用prepare函数,所以主线程不需要调用Looper.prepare()函数,然后我们也可以看到这里有行这个代码

 

 

        if (sThreadLocal.get() != null) {throw new RuntimeException("Only one Looper may be created per thread");}


在Looper.java类中,我们的Looper保存在ThreadLocal里面

 

 

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

用ThreadLocal修饰的变量,可以理解为只有当前线程可以改变这个参数,其它线程不可以改变这个参数,如果你对ThreadLocal不清楚,单独可以先看下我这篇博客的简单使用

 

 java之ThreadLocal简单使用总结    http://blog.csdn.net/u011068702/article/details/75770226

 

        if (sThreadLocal.get() != null) {throw new RuntimeException("Only one Looper may be created per thread");}

上面的代码写得很清楚了,如果当前的sThreadLocal对象里面有个Looper对象,那么就会抛出异常,而且英文也提示了,所以,一个线程为什么只能有一个Looper对象的原因,所以如果在程序里面,每个线程调用2次Looper.prepare()就会报错,我们再看这行代码

 

 

        sThreadLocal.set(new Looper(quitAllowed));


点击Looper的构造函数

 

 

    private Looper(boolean quitAllowed) {mQueue = new MessageQueue(quitAllowed);mThread = Thread.currentThread();}


里面构建了一个MessageQueue对象,上面我们分析一个线程只有一个Looper对象,那么Looper对象只构建一个,也就意味着MessageQueue对象也只构建一次,所以一个线程也只有一个MessageQueue对象的原因。

 

在main函数里面,也调用了Looper.loop()函数,后面分析这个方法

 

 

 

3、分析Handler发送消息

我们先看下Handler.java的构造方法,这个类在安卓 android.os这个目录下面

 

    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对象。

 

 

发送消息的时候我们一般都这样写
handler.sendMessage(message) 

 

所以我们点击 sendMessage方法看下

 

    public final boolean sendMessage(Message msg){return sendMessageDelayed(msg, 0);}


再点击sendMessageDelayed(msg, 0);

 

 

    public final boolean sendMessageDelayed(Message msg, long delayMillis){if (delayMillis < 0) {delayMillis = 0;}return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);}


再点击sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);

 

 

    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);}


再点击enqueueMessage(queue, msg, uptimeMillis)

 

 

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

这里msg.target就是Handler对象本身,再点击queue.enqueueMessage(msg, uptimeMillis)(在MessageQueue这个类里面)

 

 

 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("MessageQueue", 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.nextprev.next = msg;}// We can assume mPtr != 0 because mQuitting is false.if (needWake) {nativeWake(mPtr);}}return true;}


可以看出MessageQueue从而按照时间将所有的Message排序

 

然后我们不是最后还调用了Looper.loop()函数吗?点击进去

 

/*** Run the message queue in this thread. Be sure to call* {@link #quit()} to end the 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 blockif (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 loggerPrinter logging = me.mLogging;if (logging != null) {logging.println(">>>>> Dispatching to " + msg.target + " " +msg.callback + ": " + msg.what);}msg.target.dispatchMessage(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()方法里起了一个死循环,不断的判断MessageQueue中的消息是否为空,如果为空则直接return掉,然后执行queue.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 iterationint 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) {// 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 {mMessages = msg.next;}msg.next = null;if (false) Log.v("MessageQueue", "Returning message: " + msg);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 handlerboolean keep = false;try {keep = idler.queueIdle();} catch (Throwable t) {Log.wtf("MessageQueue", "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;}}

Message的出栈操作,里面可能对线程,并发控制做了一些限制等。获取到栈顶的Message对象,然后就执行这个函数

 

 

 msg.target.dispatchMessage(msg);


我么知道msg.tartget对象就是handler对象,我们点击dispatchMessage(msg)函数

 

 

    /*** Handle system messages here.*/public void dispatchMessage(Message msg) {if (msg.callback != null) {handleCallback(msg);} else {if (mCallback != null) {if (mCallback.handleMessage(msg)) {return;}}handleMessage(msg);}}


我们可以知道msg.callback  != null的时候,就执行了handleCallback(msg)

 

 

    private static void handleCallback(Message message) {message.callback.run();}


意味着这个Runable执行 run方法

 

然后还有就是也可能会执行到这里来

 

            handleMessage(msg);


点击进去,

 

 

    /*** Subclasses must implement this to receive messages.*/public void handleMessage(Message msg) {}


我们一般在主线程这样写接收消息

 

 

    public Handler mHandlerCToP = new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch(msg.what) {case 0:break;default:break;}}};

也就意味着把handleMessage()方法重写了,所以我们的代码,有地方发送消息,loop()不断分发消息,当收到了,如果接收到了,我们重写handleMessage就会掉到这个地方来,得到我需要的数据。

 

 

 

 

 

4、分析runOnUiThread方法和Handler.post方法和view的post方法

    1、分析runOnUiThread()方法

            我们一般在子线程调用这个方法也可以来更新UI

           

        runOnUiThread(new Runnable() {@Overridepublic void run() {}});

              点击进去

 

              

    public final void runOnUiThread(Runnable action) {if (Thread.currentThread() != mUiThread) {mHandler.post(action);} else {action.run();}}

             再点击mHandler.post(action) 方法

 

 

    public final boolean post(Runnable r){return  sendMessageDelayed(getPostMessage(r), 0);}

           点击进去

    private static Message getPostMessage(Runnable r) {Message m = Message.obtain();m.callback = r;return m;}

         看到了吗?其实最后还是到了发送消息这里,一开始我们不是分析了Looper.loop()里面的dispatchMessage()方法吗?

我么知道msg.tartget对象就是handler对象,我们点击dispatchMessage(msg)函数/*** Handle system messages here.*/public void dispatchMessage(Message msg) {if (msg.callback != null) {handleCallback(msg);} else {if (mCallback != null) {if (mCallback.handleMessage(msg)) {return;}}handleMessage(msg);}}

         很明显程序,会走handleCallback(msg);所以会调到handler.java里面的这个方法

 

 

    private static void handleCallback(Message message) {message.callback.run();}

         这样就执行了这个Runnable

 

 

    2、分析handler.post()方法

          上面那个函数内部有handler.post()这个方法,已分析

         

    3、分析view.post()方法

          点击view.post()方法

 

    public boolean post(Runnable action) {final AttachInfo attachInfo = mAttachInfo;if (attachInfo != null) {return attachInfo.mHandler.post(action);}// Assume that post will succeed laterViewRootImpl.getRunQueue().post(action);return true;}

         可以发现其调用的就是activity中默认保存的handler对象的post方法
              

 

       

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/290808.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VS2010 安装问题积累

vs2010 SP1安装不成功 日志文件里显示&#xff1a;error 1719 windows installer service could not be accessed 解决方法&#xff1a;Start, then Run, then type regedit Go to HKEY_LOCAL_MACHINE\SYSTEM\CURRENT CONTROL SET\SERVICES\MSIserver\WOW64 …

unix环境高级编程基础知识之第二篇(3)

看了unix环境高级编程第三章&#xff0c;把代码也都自己敲了一遍&#xff0c;另主要讲解了一些IO函数&#xff0c;read/write/fseek/fcntl&#xff1b;这里主要是c函数&#xff0c;比较容易&#xff0c;看多了就熟悉了。对fcntl函数讲解比较到位&#xff0c;它可以得到和改变打…

Avalonia跨平台入门第七篇之RadioButton的模板

前面其实已经玩耍过单选按钮,只不过一直好意思分享出来;今天终于可以正大光明的分享出来了,直接看效果吧:第一次使用然后的傻傻的版本(根据单选按钮的选中状态来切换二个图片);真的好Low:样式写法和WPF没太大区别:类似WPF中的触发器,使用了附加属性:前台具体使用方式:最终简单的…

svn之bash: syntax error near unexpected token `(‘ 解决办法

1、问题 svn update *****/网易(杭州)网络有限公司SSL-20170623001 出现这个错误 bash: syntax error near unexpected token ( 2、解决办法 改成下面的就行 把svn update *****/网易’(‘杭州’)‘网络有限公司SSL-20170623001

Angular 4.x 事件管理器及自定义EventManagerPlugin

在 Angular 中如何为同一个表达式绑定多个事件呢&#xff1f;如果我们这样做可能会是这样的&#xff1a; <div><button (click, mouseover)"onClick()">Click me</button> </div>复制代码在继续分析绑定多个事件之前&#xff0c;我们先来分析…

dell服务器报内存配置不正确,DELL 服务器系统提示错误解决的若干办法

《DELL 服务器系统提示错误解决的若干办法》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《DELL 服务器系统提示错误解决的若干办法(9页珍藏版)》请在人人文库网上搜索。1、DELL 服务器有时会若硬件的改动&#xff0c;在开机以后会提示错误信息。信息一般会提示在显示…

JSP PO VO BO DTO POJO DAO解释

PO &#xff1a;persistent object持久对象 1 &#xff0e;有时也被称为Data对象&#xff0c;对应数据库中的entity&#xff0c;可以简单认为一个PO对应数据库中的一条记录。2 &#xff0e;在hibernate持久化框架中与insert/delet操作密切相关。 3 &#xff0e;PO中不应该包含任…

java之RSA和Base64加密帮助类

1、RSAUtils.java类 package com.sangfor.vpn.client.service.utils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigIn…

更强的压缩比!PostgreSQL开始支持Zstd

文 | 局长出品 | OSC开源社区&#xff08;ID&#xff1a;oschina2013&#xff09;PostgreSQL 现已通过其 TOAST 存储技术提供压缩支持&#xff0c;并且在过去的一年里构建了 LZ4 压缩支持——用于压缩 WAL、备份压缩以及其他用途&#xff0c;现在 PostgreSQL 开发者正准备通过 …

最近面试遇到的技术问题

京东性能组 1. oracle awr2. mysql 慢查询3. redis详细架构、如何插入数据4. jmeter使用及集群搭建5. nginx使用及tomcat集成 6. 数据库及sql优化的方案 7. 写存储过程 8. linux使用9. shell10. java e代驾 1. 接口测试方法2. sockets连接建立方法3. http三次握手过程 京东金融…

小程序和app用什么样的服务器,小程序和APP的本质区别是什么?哪个更值得开发?...

从微信小程序和用户见面到现在&#xff0c;这个功能已经越来越完善了&#xff0c;经过更彻底的挖掘&#xff0c;商业价值被挖掘出来了&#xff01;小程序和app有什么区别呢&#xff1f;为什么广州会更受欢迎呢&#xff1f;两者的区别首先&#xff0c;两者的区别在于&#xff0c…

jbpm6.5 环境搭建(三) 数据库 切换

2019独角兽企业重金招聘Python工程师标准>>> 经过一晚上的折腾&#xff0c;终于搞定&#xff0c;成功切换Mysql 步骤一&#xff1a; 安装mysql 数据库 创建数据库 名字为jbpm 设置用户名密码 我本地默认使用 root 步骤二&#xff1a; ** 修改配置文件 ** F:\jb…

Android之HandlerThread源码分析和简单使用(主线程和子线程通信、子线程和子线程通信)

1、先熟悉handler方式实现主线程和子线程互相通信方式&#xff0c;子线程和子线程的通信方式 如果不熟悉或者忘记了&#xff0c;请参考我的这篇博客 Android之用Handler实现主线程和子线程互相通信以及子线程和子线程之间的通信 http://blog.csdn.net/u011068702/arti…

CENTOS6.4安装vnc-server

yum install tigervnc-servervi /etc/sysconfig/vncservers只需要两类内容就可以了&#xff0c;一个是定义用户&#xff0c;一个是定义用户登录情况&#xff1a;参考&#xff1a;VNCSERVERS"1:root 2:river"VNCSERVERARGS[1]"-geometry 800x600 -nolisten tcp&q…

Avalonia跨平台入门第八篇之控件的拖放

在前面分享的几篇中咱已经玩耍了Popup、ListBox多选、Grid动态分、RadioButton模板,过程还算顺利;今天接着把把ListBox中的Item拖放到Cavans中(基于官方的Samples实现的);直接看效果吧:1、ListBox中PointerPressed、DragOver事件:2、Canvas中的Drop事件:3、控件的移除无非就是通…

sql server 之函数小技巧 整数类型为空是用空字符串替代实现

1、判空函数 说明&#xff1a;使用指定的替换值替换 NULL。 语法&#xff1a;ISNULL ( check_expression , replacement_value ) 参数&#xff1a; check_expression&#xff1a;将被检查是否为 NULL 的表达式。check_expression 可以为任何类型。 replacement_value&#xff1…

车牌识别系统连不上服务器怎么办,车牌识别系统出现故障的解决方法有哪些?...

在日常生活中&#xff0c;各个小区、商业广场、酒店、办公楼等等地方出入口装置有车牌识别系统&#xff0c;有此可见车牌识别系统的使用越来越广泛。停车场办理系统的使用给人们带来便利的同时&#xff0c;也常常会出现一些小问题。今天小编就给大家分享一下车牌识别系统遇到故…

霍夫变换

作者&#xff1a;桂。 时间&#xff1a;2017-04-24 12:18:17 链接&#xff1a;http://www.cnblogs.com/xingshansi/p/6756305.html 前言 今天群里有人问到一个图像的问题&#xff0c;但本质上是一个基本最小二乘问题&#xff0c;涉及到霍夫变换&#xff08;Hough Transform&a…

ASP.NET Core 实现基于 ApiKey 的认证

ASP.NET Core 实现基于 ApiKey 的认证Intro之前我们有介绍过实现基于请求头的认证&#xff0c;今天来实现一个基于 ApiKey 的认证方式&#xff0c;使用方式参见下面的示例Sample注册认证服务services.AddAuthentication().AddApiKey(options >{options.ApiKey "123456…

Android之调用系统分享

1、调用系统分享关键代码 private void shareImage() {Intent intent = new Intent(Intent.ACTION_SEND); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("sdcard/screenshot.png")));intent.setTyp…