Android消息机制源码分析记录

Java层的消息机制主要由MessageQueue,Handler,Looper来实现

Looper

ActivityThread类的main方法为进程的入口,也就是所说的主线程,这里开始了Looper的初始化

ActivityThread.java
public static void main(String[] args) {
 ...   
     Looper.prepareMainLooper();
     ActivityThread thread = new ActivityThread();
      if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }
...
        Looper.loop();
    } 

Looper的的主要职责是对MessageQueue的管理:初始化、轮询、退出

Looper.java
//static修饰意味着只实列化一次, Looper对象可能有很多,但ThreadLocal只有一个
static final ThreadLocal sThreadLocal = new ThreadLocal();

//虽然此方法是公共的,但用户不要使用,它是给系统调用的
public static void prepareMainLooper() {
    //与普通Looper的区别是,参数quitAllowed为false,即主线的Looper不允许退出,它是应用程序运行的根基
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }

 private static void prepare(boolean quitAllowed) {
        //通过ThreadLocal.get()方法可以获取与调用线程关联的Looper对象
       //如果调用线程已经关联了Looper会抛出异常,因为每个线程只能关联一个Looper
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        //使用ThreadLocal为调用线程关联Looper
        sThreadLocal.set(new Looper(quitAllowed));
    }
    
//实列化Looper,主要是实列化MeseageQueue,quitAllowed=false,说明此MeseageQueue不可以退出
 private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

MessageQueue初始化很简单

MessageQueue.java
  MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        //使用native方法初始化消息队列
        mPtr = nativeInit();
    }

Thread类中threadLocals字段是用来记录线程局部变量,它是线程私有的,这样就不存在并发问题,但Thread类并不管理这个字段,而是由ThreadLocal类维护。Thread类中的大部分字段是private修饰,但这个字段特意设计成包访问权限,这样同包下的ThreadLocal才能维护。

Thread.java
/* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

ThreadLocalMap是ThreadLocal的静态内部类,一个专为Thread线程局部变量而定制的hash map,也是包访问权限,这样Thread类可以使用。里面的方法、字段都是私有,只能ThreadLocal访问。ThreadLocalMap不需要访问外部类ThreadLocal,所以设计成静态内部类,这样不持有外部类引用,完成对象初始化的速度更快。

ThreadLocal.java
static class ThreadLocalMap {
      ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {
        //Entry继承了WeakReference,对ThreadLocal是弱引用,防止ThreadLocalMap生命周期过长影响ThreadLocal的垃圾回收
        table = new Entry[INITIAL_CAPACITY];
        int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
        table[i] = new Entry(firstKey, firstValue);
        size = 1;
        setThreshold(INITIAL_CAPACITY);
      }
}

为什么线程局部变量设计成一个Map呢,因为可能有多个ThreadLocal实列在同一个线程上关联不同的线程本地变量,所以这个Map维护的是这样的,Object>键值对,为什么Key是ThreadLocal这样的泛型,对ThreadLocal来说对象类型是确定的,但对Map来说ThreadLocal这个泛型是不确定的,如下表。

Key Value
ThreadLocal Looper
ThreadLocal Apple
ThreadLocal Orange

再看prepare里的set(Looper)方法,就是把,Looper>键值对存在当前调用线程也就是主线程的ThreadLocalMap里。get()方法从当前调用线程也就是主线程的ThreadLocalMap中找到key为ThreadLocal的那个Entry并取出Looper对象。在消息机制里,ThreadLocal默默地帮我们处理了很多细节,实现了线程和Looper的关联

上面的main方法有一个getHandler调用,返回的是无参构造的Handler。Handler的特点是必须关联一个Looper,如果没有传Looper作为参数,那么当前调用线程必须事先关联好Looper,如上所述。

Handler.java
    public Handler() {
        this(null, false);
    }
    public Handler(Callback callback, boolean async) {
      //获取的就是当前调用线程关联的Looper        
        mLooper = Looper.myLooper();
       // 没有Looper会抛异常,也就是初始化handler之前必须有Looper
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        //使用的是Looper中的消息队列
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

Looper.java
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }

至此Looper、MessageQueue、Handler都完成了初始化和关联,Loop.loop开始无限循环获取要处理的消息

Looper.java
   public static void loop() {
       ...
        final MessageQueue queue = me.mQueue;
      ...
        //开始在这个线程上无线循环
        for (;;) {
        //从消息队列取一个消息,如果没有会阻塞在此不会占用CPU
            Message msg = queue.next(); 
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
           ...           
            try {
             //调用msg相关的handler处理,msg持用handler引用也是导致内存泄漏的地方
                msg.target.dispatchMessage(msg);
            } finally {
               ...
            }
           ...
          // 回收消息到消息池中复用
            msg.recycleUnchecked();
        }
    }

MessageQueue

MQ Java层是一个包装,核心功能在native,当Looper开始运作,真正在工作的是MQ ,它使用链表来维持一个以时间排序的消息队列。MQ的作用主要就是压入msg,取出msg,移除msg等

MQ的主要工作1:从队头取出Message
 Message next() {
        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();
            }
            //调用本地方法轮询一次,没有消息会阻塞,直到超时或被唤醒,不会占用CPU
            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;
                // 对于取出来的MSG,首先要判断它是不是一个barrier(特征:handler=null)用来阻塞同步消息的
                //应用程序很少使用这种异步MSG,系统使用场景?->ViewRootImpl。
                if (msg != null && msg.target == null) {
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    // 如果是的话找到后面第一条异步MSG(特征:msg.isAsynchronous())
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
             
                if (msg != null) {
                 //如果msg是异步消息,那么prevMsg != null,如果是同步消息,那么prevMsg == 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;
                        msg.markInUse();
                        return msg;
                    }
                } else {
                  //如果消息队列被barrier阻塞却找不到异步消息,MQ会继续阻塞而不会挑一个同步消息执行
                    nextPollTimeoutMillis = -1;
                }

                if (mQuitting) {
                    //此时未处理的消息已清空,调用native方法销毁消息队列
                    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);
            }
            //如果队列中没有消息或者有消息但没有到执行时间,会给IdleHandler一次回调的机会,
            //也就是在队列不繁忙的时候执行一些操作
           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;
        }
    }
MQ的主要工作2:向消息队列插入Message
MessageQueue.java
 boolean enqueueMessage(Message msg, long when) {
       ...
        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.
                //判断唤醒队列的条件:已阻塞 消息头是barrier,插入的消息是异步的
                //上面的分析可知,barrier会阻塞同步消息,需要唤醒MQ来处理这个异步消息
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                //找到在单链表中要插入的位置
                for (;;) {
                    prev = p;
                    p = p.next;
                    //p==null 插入队尾,一种是队列中只有一个消息头,另外一种情况是msg的when大于队列中所有when,要插到队尾
                    //when
MQ的主要工作3:按条件移除消息

除了移除所有消息,还可根据handler、what 、obj等参数移除msg。 为什么分两个循环 , 一个循环应该就能完成 原因在于会丢掉队头消息,

    void removeMessages(Handler h, int what, Object object) {
        if (h == null) {
            return;
        }
        synchronized (this) {
            Message p = mMessages;
            //当从队头开始就符合条件时,msg会被移除 这样我们会丢失队头,所以在回收的同时也要重新设置队头
            // Remove all messages at front.
            while (p != null && p.target == h && p.what == what
                   && (object == null || p.obj == object)) {
                Message n = p.next;
                //mMessages设置新队头
                mMessages = n;
                p.recycleUnchecked();
                p = n;
            }
            // Remove all messages after front.
            //在确定新队头后 回收后面符合条件的msg 并从队列移除
            while (p != null) {
                Message n = p.next;
                if (n != null) {
                    if (n.target == h && n.what == what
                        && (object == null || n.obj == object)) {
                        Message nn = n.next;
                        n.recycleUnchecked();
                        p.next = nn;
                        continue;
                    }
                }
                p = n;
            }
        }
    }
MQ的最后的工作:退出

注意:主线程的MQ是不可以退出的,初始化的参数mQuitAllowed=false。带Looper的子线程则在使用结束后必须手动退出,否则线程无法终止,导致整个引用链都无法回收

void quit(boolean safe) {
        if (!mQuitAllowed) {
            throw new IllegalStateException("Main thread not allowed to quit.");
        }
        synchronized (this) {
           ...
            if (safe) {
                removeAllFutureMessagesLocked();
            } else {
                //不安全退出相对简单,不管三七二十一把队列的所有msg回收掉
                removeAllMessagesLocked();
            }
            //?
            nativeWake(mPtr);
        }
    }

    //主要看看安全退出 策略是把队列中比当前时间大的msg回收掉
    private void removeAllFutureMessagesLocked() {
        final long now = SystemClock.uptimeMillis();
        Message p = mMessages;
        if (p != null) {
            //如果队头的时间都比now大,说明全都可以移除
            if (p.when > now) {
                removeAllMessagesLocked();
            } else {
                //否则找到比now大的第一个msg,也就是n
                Message n;
                for (;;) {
                    n = p.next;
                    if (n == null) {
                        return;
                    }
                    if (n.when > now) {
                        break;
                    }
                    p = n;
                }
                //p的后面就是n,从n开始都是要移除的 断开链接
                p.next = null;
                //从n开始回收
                do {
                    p = n;
                    n = p.next;
                    p.recycleUnchecked();
                } while (n != null);
            }
        }
    }
MQ的次要工作1:向消息队列插入同步屏障
private int postSyncBarrier(long when) {
        // Enqueue a new sync barrier token.
        // We don't need to wake the queue because the purpose of a barrier is to stall it.
        synchronized (this) {
            final int token = mNextBarrierToken++;
            final Message msg = Message.obtain();
            //msg的target为null,这是我们识别同步屏障的特征
            msg.markInUse();
            msg.when = when;
            msg.arg1 = token;

            Message prev = null;
            Message p = mMessages;
            //同样要给同步屏障msg找到需要插入的位置,如果when为0 明显插到队头
            if (when != 0) {
                //p.when<=when 表示链表中的p比要插入的msg时间小,msg需要往后找位置
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
            }
            //找到了能插入的位置 
            if (prev != null) { // invariant: p == prev.next
                msg.next = p;
                prev.next = msg;
            } else {
            //prev==null 说明插在队头 when比队列中时间都小
                msg.next = p;
                mMessages = msg;
            }
            return token;
        }
    }
MQ的次要工作2:移除同步屏障
public void removeSyncBarrier(int token) {
        // Remove a sync barrier token from the queue.
        // If the queue is no longer stalled by a barrier then wake it.
        synchronized (this) {
            Message prev = null;
            Message p = mMessages;
            //一般来说同步屏障就在队头,如果在队中说明还没起屏障作用就要把它移除
            while (p != null && (p.target != null || p.arg1 != token)) {
                prev = p;
                p = p.next;
            }
            if (p == null) {
                throw new IllegalStateException("The specified message queue synchronization "
                        + " barrier token has not been posted or has already been removed.");
            }
            final boolean needWake;
            //如果prev!=null说明在队中,同步屏障还没起作用 没有阻塞队列 剔除p 不需要唤醒
            if (prev != null) {
                prev.next = p.next;
                needWake = false;
            } else {
                //同步屏障在队头,将队头指向同步屏障msg的下一个 即移除barrier
                mMessages = p.next;
                //为啥下一个msg为空需要唤醒队列,不解??
                needWake = mMessages == null || mMessages.target != null;
            }
            p.recycleUnchecked();

            // If the loop is quitting then it is already awake.
            // We can assume mPtr != 0 when mQuitting is false.
            if (needWake && !mQuitting) {
                nativeWake(mPtr);
            }
        }
    }
Handler

Handler是个辅助类,方便其它线程发送消息给自己所在线程处理处理,但自己并不维护消息队列,而是交由MessageQueue管理,它不仅管理Java 层消息,也管理Natvie层消息。Looper绑定唯一Thread,Handler绑定唯一的Looper,所以如果你希望msg在哪个线程执行,使用和那个线程绑定的Handler发送消息即可.

消息处理

Handler提供的插入、查询、删除msg方法,最终都是由MQ实现的, 以插入为例

Handler.java
//最终都调用这个方法发送消息
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);
    }
    
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        //设置msg由谁处理,就是发送它的handler,谁发送谁处理,
        //发送可以在任意线程调用,处理一定在和handler关联的线程上
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        由queue负责将消息入队列
        return queue.enqueueMessage(msg, uptimeMillis);
    }    
特殊操作

Handler中有一个特殊的方法,它能阻塞调用者的线程,直到handler所在的线程把这个msg执行完毕

Handler.java
    public final boolean runWithScissors(@NonNull Runnable r, long timeout) {
       ...
        //如果调用线程和Handler所在线程是同一个,直接执行后返回
        if (Looper.myLooper() == mLooper) {
            r.run();
            return true;
        }
        //否则使用BlockingRunnable
        BlockingRunnable br = new BlockingRunnable(r);
        return br.postAndWait(this, timeout);
    }

主要看BlockingRunnable的实现

Handler.java
     private static final class BlockingRunnable implements Runnable {
        private final Runnable mTask;
        private boolean mDone;

        public BlockingRunnable(Runnable task) {
            mTask = task;
        }
        @Override
        public void run() {
            //被handler执行了 最后设置mDone=true 唤醒等待线程 跳出无限循环结束postAndWait,也就解除了阻塞
            //为什么looper退出的时候最好使用安全模式,从上面的分析可知,安全模式只移除quit时刻之后的msg,这样能
            //保证BlockingRunnable这样的msg能执行,阻塞也能解除。如果是非安全模式,所有msg被移除,包括这个BlockingRunnable,
            //那么此run方法就永远不会执行,此线程也就有可能一直阻塞
            try {
                mTask.run();
            } finally {
                synchronized (this) {
                    mDone = true;
                    notifyAll();
                }
            }
        }

        public boolean postAndWait(Handler handler, long timeout) {
            //先发到handler所在线程的MQ中,等待执行 如果MQ已退出,压入失败 返回false
            if (!handler.post(this)) {
                return false;
            }
            //阻塞当前线程
            synchronized (this) {
                if (timeout > 0) {
                    //设置了超时等待
                    final long expirationTime = SystemClock.uptimeMillis() + timeout;
                    while (!mDone) {
                        long delay = expirationTime - SystemClock.uptimeMillis();
                        //没等待之前肯定大于0,等待苏醒后先检查mDone,如果还是false,看看有没有超时,如果超了就不等待了
                        if (delay <= 0) {
                            return false; // timeout
                        }
                        //进入等待状态,等待指定的delay后苏醒检查mDone,有可能delay还没到就被唤醒
                        try {
                            wait(delay);
                        } catch (InterruptedException ex) {
                        }
                    }
                } else {
                //没有设置超时 无限等待
                    while (!mDone) {
                        try {
                            wait();
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            }
            return true;
        }
    }

你可能感兴趣的:(Android消息机制源码分析记录)