理解Handler消息机制

Handler并不是专门用于更新UI,它只是常被开发者用来更新UI。

Android的消息机制主要指Handler的运行机制,底层需要MessageQueue和Looper的支撑。MessageQueue是采用单链表的数据结构来存储消息列表的,Looper为消息循环。由于MessageQueue是一个消息的存储单元,不能处理信息,Looper就填补了这一功能,Looper会以无限循环的方式去查找是否有新消息,有就处理,没有就等待。Looper还有一个ThreadLocal,可以在每个线程中存储数据。通过ThreadLocal可以获取每个线程的Looper,一般线程默认是没有Looper,除了UI线程,所以就必须创建Looper。

一、Android消息机制概述

Handler的主要功能是将一个任务切换到某个指定的线程中去操作,这是因为Android规定UI操作只能在主线程,不然就会抛出异常,原因是因为ViewRootImpl对UI操作做了验证,通过ViewRootImpl的checkThread方法来完成。

系统提供Handler的主要原因是为了解决子线程中无法访问到UI的矛盾。

这是因为Android的UI控件不是线程安全的,如果多线程访问会导致UI处于不可预期状态,如果加上锁机制,会有两个问题:
- 加上锁会导致让UI访问的逻辑变得复杂
- 降低UI的访问效率

一般创建Handler的时候需要有Looper,不然会出现异常。

image

Handler创建以后,会同内部的Looper以及MessageQueue一起协同工作,然后Handler的post方法将一个Runanble投递到Handler内部的Looper中去处理,其实post也是通过send方法来完成的,当调用send方法时,会调用MessageQueue的enqueueMessage方法将消息放到消息队列中,然后Looper发现有新的消息来时,则会处理这个消息,最后消息中的Runnable或者Handler的handleMessage方法会被调用。注意Looper是运行在创建Handler所在的线程中的,这样一来Handler中的业务逻辑就被切换到创建Handler所在的线程中去执行。


二、Android的消息机制分析

1.ThreadLocal的工作原理

ThreadLocalThreadLocal是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据存储以后,只有在指定线程中可以获取到存储的数据,对于其它线程来说无法获取到数据。ThreadLocal另一个使用场景是复杂逻辑下的对象传递,比如监听器的传递,有些时候一个线程中的任务过于复杂,这可能表现为函数调用栈比较深以及代码入口的多样性,在这种情况下,我们又需要监听器能够贯穿整个线程的执行过程,这个时候可以怎么做呢?其实就可以采用ThreadLocal,采用ThreadLocal可以让监听器作为线程内的全局对象而存在,在线程内部只要通过get方法就可以获取到监听器。

介绍了那么多ThreadLocal的知识,可能还是有点抽象,下面通过实际的例子为大家演示ThreadLocal的真正含义。首先定义一个ThreadLocal对象,这里选择Boolean类型的,如下所示:

private ThreadLocal<Boolean>mBooleanThreadLocal = new ThreadLocal<Boolean>();

然后分别在主线程、子线程1和子线程2中设置和访问它的值,代码如下所示:

mBooleanThreadLocal.set(true);
Log.d(TAG, "[Thread#main]mBooleanThreadLocal=" + mBooleanThreadLocal.get());

new Thread("Thread#1") {
    @Override
    public void run() {
        mBooleanThreadLocal.set(false);
        Log.d(TAG, "[Thread#1]mBooleanThreadLocal=" + mBooleanThreadLocal.get());
    };
}.start();

new Thread("Thread#2") {
    @Override
    public void run() {
        Log.d(TAG, "[Thread#2]mBooleanThreadLocal=" + mBooleanThreadLocal.get());
    };
}.start();

在上面的代码中,在主线程中设置mBooleanThreadLocal的值为true,在子线程1中设置mBooleanThreadLocal的值为false,在子线程2中不设置mBooleanThreadLocal的值,然后分别在3个线程中通过get方法去mBooleanThreadLocal的值,根据前面对ThreadLocal的描述,这个时候,主线程中应该是true,子线程1中应该是false,而子线程2中由于没有设置值,所以应该是null,安装并运行程序,日志如下所示:

D/TestActivity(8676):[Thread#main]mBooleanThreadLocal=true
D/TestActivity(8676):[Thread#1]mBooleanThreadLocal=false
D/TestActivity(8676):[Thread#2]mBooleanThreadLocal=null

从上面日志可以看出,虽然在不同线程中访问的是同一个ThreadLocal对象,但是它们通过ThreadLocal来获取到的值却是不一样的,这就是ThreadLocal的奇妙之处。结合这这个例子然后再看一遍前面对ThreadLocal的两个使用场景的理论分析,大家应该就能比较好地理解ThreadLocal的使用方法了。ThreadLocal之所以有这么奇妙的效果,是因为不同线程访问同一个ThreadLocal的get方法,ThreadLocal内部会从各自的线程中取出一个数组,然后再从数组中根据当前ThreadLocal的索引去查找出对应的value值,很显然,不同线程中的数组是不同的,这就是为什么通过ThreadLocal可以在不同的线程中维护一套数据的副本并且彼此互不干扰。

ThreadLocal是一个泛型类,它的定义为public class ThreadLocal,只要弄清楚ThreadLocal的get和set方法就可以明白它的工作原理。
首先看ThreadLocal的set方法,如下所示:

public void set(T value) {
    Thread currentThread = Thread.currentThread();
    Values values = values(currentThread);
    if (values == null) {
        values = initializeValues(currentThread);
    }
    values.put(this, value);
}

在上面的set方法中,首先会通过values方法来获取当前线程中的ThreadLocal数据,如果获取呢?其实获取的方式也是很简单的,在Thread类的内容有一个成员专门用于存储线程的ThreadLocal的数据,如下所示:ThreadLocal.Values localValues,因此获取当前线程的ThreadLocal数据就变得异常简单了。如果localValues的值为null,那么就需要对其进行初始化,初始化后再将ThreadLocal的值进行存储。下面看下ThreadLocal的值到底是怎么localValues中进行存储的。在localValues内部有一个数组:private Object[] table,ThreadLocal的值就是存在在这个table数组中,下面看下localValues是如何使用put方法将ThreadLocal的值存储到table数组中的,如下所示:

void put(ThreadLocal<?> key, Object value) {
    cleanUp();

    // Keep track of first tombstone. That's where we want to go back
    // and add an entry if necessary.
    int firstTombstone = -1;

    for (int index = key.hash & mask;; index = next(index)) {
        Object k = table[index];

        if (k == key.reference) {
            // Replace existing entry.
            table[index + 1] = value;
            return;
        }

        if (k == null) {
            if (firstTombstone == -1) {
                // Fill in null slot.
                table[index] = key.reference;
                table[index + 1] = value;
                size++;
                return;
            }

            // Go back and replace first tombstone.
            table[firstTombstone] = key.reference;
            table[firstTombstone + 1] = value;
            tombstones--;
            size++;
            return;
        }

        // Remember first tombstone.
        if (firstTombstone == -1 && k == TOMBSTONE) {
            firstTombstone = index;
        }
    }
}

面的代码实现数据的存储过程,这里不去分析它的具体算法,但是我们可以得出一个存储规则,那就是ThreadLocal的值在table数组中的存储位置总是为ThreadLocal的reference字段所标识的对象的下一个位置,比如ThreadLocal的reference对象在table数组的索引为index,那么ThreadLocal的值在table数组中的索引就是index+1。最终ThreadLocal的值将会被存储在table数组中:table[index + 1] = value。

上面分析了ThreadLocal的set方法,这里分析下它的get方法,如下所示:

public T get() {
    // Optimized for the fast path.
    Thread currentThread = Thread.currentThread();
    Values values = values(currentThread);
    if (values != null) {
        Object[] table = values.table;
        int index = hash & values.mask;
        if (this.reference == table[index]) {
            return (T) table[index + 1];
        }
    } else {
        values = initializeValues(currentThread);
    }

    return (T) values.getAfterMiss(this);
}

可以发现,ThreadLocal的get方法的逻辑也比较清晰,它同样是取出当前线程的localValues对象,如果这个对象为null那么就返回初始值,初始值由ThreadLocal的initialValue方法来描述,默认情况下为null,当然也可以重写这个方法,它的默认实现如下所示:

/**
 * Provides the initial value of this variable for the current thread.
 * The default implementation returns {@code null}.
 *
 * @return the initial value of the variable.
 */
protected T initialValue() {
    return null;
}

从ThreadLocal的set和get方法可以看出,它们所操作的对象都是当前线程的localValues对象的table数组,因此在不同线程中访问同一个ThreadLocal的set和get方法,它们对ThreadLocal所做的读写操作仅限于各自线程的内部,这就是为什么ThreadLocal可以在多个线程中互不干扰地存储和修改数据,理解ThreadLocal的实现方式有助于理解Looper的工作原理。

2.消息队列的工作原理

MessageQueue主要包含两个操作:插入和读取,插入和读取分为enqueueMessage和next,其中enqueueMessage是往消息队列中插入一条消息,next是从消息队列中去出一条消息并从消息队列中移除,MessageQueue实际上是使用单链表来实现的。

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

}

它主要是实现单链表的插入操作。

Message next() {
        ....
        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;
                }
            }
        }

}

next是一个无限循环的方法,如果没有消息,则一直阻塞,如果有消息则继续运行。

3.Looper的工作原理

Looper在Android的消息机制中就是用来进行消息循环的。它会不停地循环,去MessageQueue中查看是否有新消息,如果有消息就立刻处理该消息,否则就一直等待。

首线来看下Looper的构造方法:

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

通过Looper.prepare()即可为当前线程创建一个Looper,接着通过Looper.loop()来开启消息循环:

new Thread("Thread 2"){
    public void run(){
        Looper.prepare();
        Handler handler = new Handler();
        Looper.loop();
    }
}

Looper中除了prepare方法外,还提供了prepareMainLooper方法,这个方法主要是给主线程创建Looper使用的,即是属于ActivityThread的Looper,其本质也是通过prepare来实现的,同时Looper也提供一个getMainLooper方法,通过它可以在任何地方获取ActivityThread的Looper。Looper也是可以退出的,它提供了quit和quitSafely方法来退出,二者的区别是:quit直接退出Looper,quitSafely只是设置一个退出标志,等消息队列中的消息处理完毕,就会退出Looper,这时候Handler的send方法返回false。如果在子线程中手动创建了Looper,要记得退出Looper,不然这个线程会一直处于等待转态,如果退出了,这个线程也会被终止。

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

Looper的loop方法也很好理解,loop是一个死循环的方法,唯一跳出死循环的方式是MessageQueue的next方法返回null。当Looper的quit方法被执行的时候,Looper会调用MessageQueue的quit或者quitSafely方法来通知消息队列退出,当被标记退出,消息队列就会返回null。loop的阻塞是因为MessageQueue的next方法阻塞造成的。如果MessageQueue的next方法返回新消息了,那就会调用msg.target.dispatchMessage(msg)来处理,其中msg.target是Handler对象,dispatchMessage虽然是通过Handler调用的,但是是在创建Handler时所使用的Looper中执行的,也就是在当前Looper中或者线程中执行的,这样就可以实现线程的切换。

4. Hanler的工作原理

Handler的工作主要包含消息的发送和接受过程。消息的发送可以通过post或者send的一系列方法来实现,如下所示:

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

    /**
     * Sends a Message containing only the what value.
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendEmptyMessage(int what)
    {
        return sendEmptyMessageDelayed(what, 0);
    }

    /**
     * Sends a Message containing only the what value, to be delivered
     * after the specified amount of time elapses.
     * @see #sendMessageDelayed(android.os.Message, long) 
     * 
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageDelayed(msg, delayMillis);
    }

    /**
     * Sends a Message containing only the what value, to be delivered 
     * at a specific time.
     * @see #sendMessageAtTime(android.os.Message, long)
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */

    public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageAtTime(msg, uptimeMillis);
    }

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

Handler的发送消息就是向MessageQueue队列插入一条消息,然后MessageQueue就会调用next方法返回这条消息给Looper,Looper会开始交由Handler处理,即Handler的dispatchMessage方法会被调用,dispatchMessage的实现如下:

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

Handler的消息处理过程如下:

  • 首先先检查Message的callback是否为空,不为空就通过handleCallback来处理,Message的callback是一个Runnable对象,实际上就是Handler的post方法所传递的Runnable参数,handlerCallback的逻辑如下:
    private static void handleCallback(Message message) {
        message.callback.run();
    }
  • 其次检查mCallback是否为null,不为null就调用mCallback的handleMessage方法来处理,Callback是个接口,定义如下:
    public interface Callback {
        public boolean handleMessage(Message msg);
    }
  • 通过Callback可以采用如下方法创建Handler handler = new Handler(callable)。在日常开发中,创建Handler最常见的方式是派生一个Handler的子类并且重写它的handleMessage方法,另外一种是传入一个Callback,并实现callback的handleMessage方法。

  • 最后调用Handler的handleMessage方法来处理消息。

处理过程的流程图:

image

Handler还有一个特殊的构造方法,通过一个特定的Looper来构造Handler,实现一些特殊的功能。


三、主线程的消息循环

Android的主线程是ActivityThread,主线程的方法入口是mian方法,main方法通过Looper.prepareMainLooper()来创建主线程的Looper和MessageQueue,并通过Looper.loop()来启动循环。主线程的消息循环开始后,需要一个Handler来进行处理和发送,这个就是ActivityThread.H,内部有一组消息类型。ActivityThread通过ApplicationThread和AMS进行通信,AMS一进程间通信完成ActivityThread的请求后回调ApplicationThread的Binder方法,然后像H发送消息,H收到后会将ApplicationThread中的逻辑切换到ActivityThread中去执行,即切换到主线程中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值