Android消息机制(Handler、MessageQueue和Looper三者的工作原理)

版权声明:本文为博主石月的原创文章,转载请注明出处 https://blog.csdn.net/liuxingrong666/article/details/84672308

Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程。messagequeue意思是消息队列,它内部存储一组消息,有插入和删除的功能,其实内部是以单链表的形式来实现队列功能的。looper的意思是循环,它的主要功能是循环读取messagequeue里面的消息,然后加以处理,如果暂时还没有消息,则looper会一直等待。在android日常开发中,我们经常利用handler将工作线程切换到UI线程从而达到更新UI的目的。

在handler机制中,每个线程都会对应一个looper,但线程默认是没有looper的,需要自己创建,而我们在android的UI线程中可以直接使用handler是因为UI线程在创建的时候已经初始化了looper,然后looper对象是通过ThreadLocal保存在对应thread的threadlocalmap中的。ThreadLocal是什么?ThreadLocal不是一个线程,它的作用是可以为每个不同的线程存储数据,这些不同线程的数据互不干扰,像我们的looper就是通过它来保存在各自线程中的,并且可以通过threadlocal来获取每个线程的looper。下面我们来讲解它们各自的工作原理。

ThreadLocl的工作原理

先来看一个例子:

public class ThreadLocalDemo {

    private java.lang.ThreadLocal<String> booleanThreadLocal = new ThreadLocal<>();

    public void test() {

        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                return false;
            }
        });

        booleanThreadLocal.set("主线程");

        LogUtils.d(Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());

        new Thread("线程01") {
            @Override
            public void run() {
                super.run();
                booleanThreadLocal.set("线程01");
                Log.d("tag", Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());
            }
        }.start();

        new Thread("线程02") {
            @Override
            public void run() {
                super.run();
                //booleanThreadLocal.set("线程02");
                Log.d("tag", Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());
            }
        }.start();

    }
}

执行结果:

通过上面的例子执行结果来看,threadlocal确实可以为不同的线程保存不同的结果,下面我们来看看它的源码是怎么实现的:

    public void set(T value) {
        //获取当前线程
        Thread t = Thread.currentThread();
        //获取当前线程的threadlocalmap
        ThreadLocalMap map = getMap(t);
        //如果threadlocalmap不为null,那么将value值保存,其中key值为当前threadlocal
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

   //返回线程的threadlocalmap对象 
   ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

  //创建对应线程的threadlocalmap实例,并且保存对应的值
  void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

其实这里的关键是将value值保存到当前线程的threadlocalmap中去,而且对应的key是threadlocal本身,下面我们来看看threadlocal的get方法:

   public T get() {
        //获取对应的线程
        Thread t = Thread.currentThread();
        //获取对应线程的threadlocalmap
        ThreadLocalMap map = getMap(t);
        //如果不为null,将取出key为当前threadlocal对应的value
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        //threadlocalmap为null的时候,返回setInitialValue的值,其实就是null
        return setInitialValue();
    }

    private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
        return value;
    }

    protected T initialValue() {
        return null;
    }

get方法其实就是返回我们保存的value值,当然如果我们之前没有对应set方法保存对应值的话,就返回null,这就是为什么上面的那个例子中,线程02会返回null的原因了。好了,其实threadlocal还是比较简单的。

MessageQueue的工作原理

messagequeue的插入和读取操作内部是由单链表结构来实现的,可能是因为单链表结构在插入和删除上效率比较高吧。插入用enqueueMessage方法,读取用next方法来实现,下面我们来看看他们的源码:

 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.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            // We can assume mPtr != 0 because mQuitting is false.
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }

上面是消息的插入操作,注意参数when代表这个消息何时取出来处理。下面我们看看,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) {
                        // 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 (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;
        }
    }

可以看到,next是一个无限循环的方法,当取到消息的时候返回msg,没有消息的时候,会一直阻塞直到有消息。注意上面的一个判断,如果now<msg.when,就先不取msg,其实就是说这个消息还没到执行时间,所以暂时不会取出来。还有当looper执行退出的时候,即上面的mQuitting为true的时候,此时next方法返回null。理解了messagequeue,下面我们来看看Looper的工作原理。

Looper的工作原理

looper在消息机制中扮演者消息循环的角色,就是说它会不停地从消息队列中查看有没有新消息,如果取出了新消息就会马上处理,否则就一直阻塞在那里。我们知道,开启looper的工作,需要在对应的线程中通过Looper.prepare()创建一个looper,然后通过loop()方法来开启消息的循环,我们来看看这个两个方法的源码:

    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        //一个线程只能创建一个looper
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        //创建一个looper实例,通过threadlocal保存起来
        sThreadLocal.set(new Looper(quitAllowed));
    }

来看看looper的构造方法:

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

可以看到,构造方法中会创建一个消息队列和获取当前looper的线程。looper还提供了prepareMainLooper方法,这个方法是用来给主线程ActivityThread创建looper使用的,当然其本质也是通过prepare方法来实现的。由于主线程的looper比较特殊,所以其提供了getMainLooper方法,用来在任何地方都可以获取主线程的looper对象。looper提供了两个退出方法:quit和quitSafely,其中quit是立即退出,quitSafely是等消息队列中的消息处理完毕以后再安全退出。如果我们手动创建了looper,在不用的时候需要我们手动去退出,否则这个子线程会一直处于等待状态。下面我们来看看looper最关键的方法loop,只有调用了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 (;;) {
            //调用消息队列的next方法取消息
            Message msg = queue.next(); // might block
            //如果消息为null,结束方法
            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 slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;

            final long traceTag = me.mTraceTag;
            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
            }
            final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            final long end;
            try {
                //调用对应handler对象的方法,将消息递交给handler处理
                msg.target.dispatchMessage(msg);
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            if (slowDispatchThresholdMs > 0) {
                final long time = end - start;
                if (time > slowDispatchThresholdMs) {
                    Slog.w(TAG, "Dispatch took " + time + "ms on "
                            + Thread.currentThread().getName() + ", h=" +
                            msg.target + " cb=" + msg.callback + " msg=" + msg.what);
                }
            }

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

loop方法还是比较好理解的,首先这是一个死循环,唯一一个跳出循环的条件是取到的消息为null,就是当我们调用了looper的退出方法的时候,同时也会去调用messagequeue的退出方法,此时next方法就会返回一个null值。所以说我们不用的时候必须要手动退出looper,不然这个循环会一直持续下去。如果我们取出了新消息,后面就会调用msg.target.dispatchMessage(msg),这里的target就是发送消息的handler对象,这样消息又交给了handler来处理了。值得注意的是,我们取出消息调用handler的dispatchMessage是在looper的loop方法中执行的,而looper对象是保存在创建时候的那个线程中的,所以此时就成功地实现了线程切换。讲解了消息队列和looper的工作原理,下面我们来看看Handler是怎么工作的。

Handler的工作原理

handler的工作主要包括消息的发送和接收,消息的发送主要用post和send系列方法,接收一般是通过重写对应的handleMessage方法。下面我们先来看看一个例子:

public class HandlerDemo {


    //重写handler的handleMessage方法
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            LogUtils.d("handler接收到消息,what内容=" + msg.what);
        }
    };

    //通过传递一个callback实现来处理消息
    Handler handler1 = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    });

    public void sendMsg() {
        //在一个子线程中通过主线程的handler发送消息给主线程处理
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //延时两秒
                    TimeUnit.SECONDS.sleep(2);
                    //发送一个消息给handler处理
                    Message msg = new Message();
                    msg.what = 01;
                    handler.sendMessage(msg);

                } catch (InterruptedException e) {

                }
            }
        }).start();


    }

    public void post() {
        handler1.post(new Runnable() {
            @Override
            public void run() {
                LogUtils.d("handler1的post方法执行了run方法");
            }
        });
    }

}

执行结果:

可以看到,例子中创建了主线程的handler对象,演示了在子线程中通过sendMessage方法发送消息给主线程处理还有post方法的使用,下面我们来看看它们的源码是怎么实现的。

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

    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return 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);
    }

   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,这里给target赋值为handler本身。我们来看看这个消息队列是怎么来的

    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());
            }
        }
        //获取对应的looper实例
        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;
    }

可以看到,在handler的构造方法中,我们获得了线程对应的looper,如果是主线程当然就是获得主线程的looper了,然后通过looper获得消息队列mQueue。

在讲looper的时候,我们知道loop方法会一直去消息队列中取消息,一旦取出新消息,就会调用handler的dispatchMessage方法,下面我们来看看这个方法:

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

其中上面的msg.callback就是post方法中的runnable对象,我们来看看:

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

再来看看handleCallback(msg)

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

可以看到会调用runnable对象的run方法,这就是handler的post方法的原理。那mCallback 又是怎么来的

    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赋值
        mCallback = callback;
        mAsynchronous = async;
    }

    //Callback接口
    public interface Callback {
        /**
         * @param msg A {@link android.os.Message Message} object
         * @return True if no further handling is desired
         */
        public boolean handleMessage(Message msg);
    }

可以看到,其实这个mCallback就是我们在例子中创建handler1的时候传递的参数。如果这个回调方法为null,我们就调用handleMessage(msg)方法处理,这是一个public方法里面为空实现,需要我们自己重写去处理,这个就是我们例子中创建handler的时候实现。

好了,关于android消息机制的工作原理,通过上面的讲解我们大概也清楚是怎么一回事了。 

猜你喜欢

转载自blog.csdn.net/liuxingrong666/article/details/84672308