android异步消息机制 源码层面彻底解析(1)

android异步消息机制 源码层面彻底解析(1),第1张

概述android异步消息机制 源码层面彻底解析(1) Handler.Message.Loopler.MessageQueen 首先看一下我们平常使用Handler的一个最常见用法. Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //这里进行一些UI *** 作等处理 } new Thread(new Runnable() { @Override public void run() {

Handler、Message、Loopler、MessageQueen

首先看一下我们平常使用Handler的一个最常见用法。

Handler handler =new Handler(){    @OverrIDe    public voID handleMessage(Message msg) {      super.handleMessage(msg);      //这里进行一些UI *** 作等处理    }     new Thread(new Runnable() {      @OverrIDe      public voID run() {        Message message = Message.obtain();        ........        handler.sendMessage(message);      }    });  };

看一下handler的构造函数的源码

public Handler() {  this(null,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;  }

看到当mLooper == null时会抛一个“Can't create handler insIDe thread that has not called Looper.prepare()”这个异常,所以我们在创建handler实例前首先需要调用Looper.prepare()

public static voID prepare() {    prepare(true);}//将looper保存到ThreadLocal中,这里可以把ThreadLocal理解为一个以当前线程为键的Map,所以一个线程中只会有一个looperprivate 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));}//我们看到在new Looper(quitAllowed)中,创建了一个消息队列MessageQueenprivate Looper(boolean quitAllowed) {  mQueue = new MessageQueue(quitAllowed);  mThread = Thread.currentThread();}

接下来我们看handler.sendMessage(message)这个方法,从字面意思就是将信息发送出去。一般sendMessage累的方法最终都会调用sendMessageAtTime(Message msg,long uptimeMillis)这个方法

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,uptimeMillis)这个方法

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

最终又会调用MessageQueen中的queue.enqueueMessage(msg,uptimeMillis)这个方法,这里的queue就是looper构造方法中创建的那个消息队列

//MessageQueen的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);        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;  }

MessageQueen虽然名字是一个队列,但实质上他是一个单向链表,这个结构能快速进行插入和删除 *** 作。从上面源码可以看出来,主要是按照发送消息的时间顺序将msg插入到消息队列中。接下来我们就需要从消息队列中取出msg了。这时候就需要调用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 (;;) {      //不断从消息队列中取出msg      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      Printer logging = me.mLogging;      if (logging != null) {        logging.println(">>>>> dispatching to " + msg.target + " " +            msg.callback + ": " + msg.what);      }      //将msg交由handler处理      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()方法通过在一个死循环中调用Message msg = queue.next()将消息不断的从消息队列中取出来。queue.next()方法的作用就是从消息队列中取msg,唯一跳出循环的方式是MessageQueen的next方法返回了null。现在msg已经取出来,下一步就是怎样将他传递给handler了对吧。所以在死循环中还有一个方法msg.target.dispatchMessage(msg) ,而msg.target就是handler,在上面handler的enqueueMessage()方法中传入的msg.target = this,this就是handler本身,接下来就看看handler的dispatchMessage()方法

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

如果我们采用无参的构造函数创建handler,msg.callback与mCallback均为空,所以我们会调用handleMessage(msg),这样文章开头的那个实例整个流程就走完了,handleMessage(msg)会在handler实例所在的线程中执行。

//当我们通过这种方式创建handler时,dispatchMessage中的mCallback就不为null public Handler(Callback callback) {    this(callback,false); }//Callback是一个接口,里面正好也有我们需要的handleMessage(Message msg),dispatchMessage中的 if (mCallback != null) 语句内的内容,就是我们需要重写的handleMessage(Message msg)方法 public interface Callback {   public boolean handleMessage(Message msg); }
//当我们调用handler.post()方法执行异步任务时  public final boolean post(Runnable r)  {    return sendMessageDelayed(getPostMessage(r),0);  }//getPostMessage(r)这个方法中我们看到给m.callback赋值了,就是我们传入的runnable接口  private static Message getPostMessage(Runnable r) {    Message m = Message.obtain();    m.callback = r;    return m;  }//最后在handleCallback方法中我们执行了它的run方法,这也就解释了为什么在子线程中可以用handler.post(Runnable r)更新UI  private static voID handleCallback(Message message) {    message.callback.run();  }

总结

梳理整个执行过程

1.调用Looper.prepare()方法,这是创建handler所必须的。在主线程中由于ActivityThread已经通过Looper.prepareMainLooper()方法创建过looper,所以在主线程中创建handler以前无需创建looper,并通过Looper.loop()来开启主线程的消息循环。

2.通过调用handler.sendMessage(message)方法最终会执行enqueueMessage(queue,uptimeMillis),enqueueMessage又会调用MessageQueen的queue.enqueueMessage(msg,uptimeMillis),这样消息就会被添加到消息队列中。

3.调用Looper.loop()方法在死循环中执行Message msg = queue.next(),不断的将msg从消息队列中取出来,同时执行msg.target.dispatchMessage(msg),将消息传递给handler,由handler来处理,如我们调用的handleMessage就是处理消息的方式之一。

异步处理机制流程图

从子线程进行UI *** 作的几种方式

AndroID 提供了几种途径来从其他线程访问 UI 线程。以下列出了几种有用的方法:

• Activity.runOnUiThread(Runnable)
• VIEw.post(Runnable) 这里的vIEw就是我们需要改变的ui控件
• VIEw.postDelayed(Runnable,long)
• Handler.post(Runnable,long)

但是,随着 *** 作日趋复杂,这类代码也会变得复杂且难以维护。 要通过工作线程处理更复杂的交互,可以考虑在工作线程中使用 Handler 处理来自 UI 线程的消息。当然,最好的解决方案或许是扩展 AsyncTask 类,此类简化了与 UI 进行交互所需执行的工作线程任务。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的android异步消息机制 源码层面彻底解析(1)全部内容,希望文章能够帮你解决android异步消息机制 源码层面彻底解析(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145758.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存