Android Handler机制

一篇搞定Handler

  • Handler如何运转
  • Looper如何线程隔离
  • IdleHandler如何使用
  • 消息有什么讲究
Handler怎么用?

通过Handler 的sendMessage方法发送一个Message,就可以在需要的地方重写Handler的hanldeMessage()方法去处理相应逻辑,并且可以保证Handler在那个线程创建,回调就在哪个线程执行。

这里有两点需要注意:
1.线程不会错乱
2.发送消息会触发回调(生产消费模型)

那么是如何做到上述两点的,我们可以看下源码,在看源码之前,先介绍几个重要的类和他们之间的关系:
注:此图只为说明关系,很多方法和成员没有列出)
简单介绍下上图所述的关系:
Handler 对象持有Looper,MessageQueue对象,并提供对应的入队,和回调方法。
Looper对象内部持有ThreadLocal,MessageQueue对象,并提供prepare()初始化方法,looper()开启循环方法等。
MessageQueue作为消息队列数据结构提供入队,出队等方法。

那么怎么让它们工作起来那?,这里分为几个步骤:
1.为当前线程创建一个Looper对象。
2.创建并使用Handler对象可用于发送消息。
3.开启Looper中的loop()循环。

确定是这样吗?

是的

我们使用handler貌似只是用到了第二步骤啊?

没错,那是Android系统帮我们实现了第一,第三步。

我们看下ActivityThread(这个类的作用,就不多说了)类main()方法部分代码

 public static void main(String[] args) {
    //第一步 创建Looper对象
    Looper.prepareMainLooper();

    //可以理解为第二步
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);

    //第三步 开启Looper中的loop()循环
    Looper.loop();
 }

我们知道在子线程中是无法直接创建Handler对象的,但是我们知道了这几个步骤后就可以使用了:

知道了怎么使用Handler,那么它到底是怎么实现的那?
我们看下sendMessage()到底干了什么

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

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

 public boolean sendMessageAtTime(@NonNull 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(@NonNull MessageQueue queue, @NonNull Message msg,
            long uptimeMillis) {
        msg.target = this;
        msg.workSourceUid = ThreadLocalWorkSource.getUid();

        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

我们可以看到sendMessage()通过一系列的方法调用最终到了MessageQueue中的enqueueMessage()方法,上代码:

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) {
               //插入消息
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                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; 
                prev.next = msg;
            }
            if (needWake) {
                //唤醒loop()
                nativeWake(mPtr);
            }
        }
        return true;
    }

MessageQueue是一个消息队列,enqueueMessage()这个方法,是根据Message的when(消息真正的发送时机)把消息插入到队列的对应位置,并唤醒loop()继续循环。

额,还有一个loop()?
是的没错,Looper对象是由系统在第一步就创建好了的,loop()循环方法是在第三步由系统调用可不要忘记,loop()方法不断从messagequeue中取出Message,Message中的traget对象所引用的又是第二部创建的Handler,Handler调用它的dispatchMessage()方法把消息回调回去
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();

        // Allow overriding a threshold with a system prop. e.g.
        // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
        final int thresholdOverride =
                SystemProperties.getInt("log.looper."
                        + Process.myUid() + "."
                        + Thread.currentThread().getName()
                        + ".slow", 0);

        boolean slowDeliveryDetected = false;

        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);
            }
            // Make sure the observer won't change while processing a transaction.
            final Observer observer = sObserver;

            final long traceTag = me.mTraceTag;
            long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
            long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
            if (thresholdOverride > 0) {
                slowDispatchThresholdMs = thresholdOverride;
                slowDeliveryThresholdMs = thresholdOverride;
            }
            final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
            final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);

            final boolean needStartTime = logSlowDelivery || logSlowDispatch;
            final boolean needEndTime = logSlowDispatch;

            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
            }

            final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
            final long dispatchEnd;
            Object token = null;
            if (observer != null) {
                token = observer.messageDispatchStarting();
            }
            long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
            try {
                //分发消息
                msg.target.dispatchMessage(msg);
                if (observer != null) {
                    observer.messageDispatched(token, msg);
                }
                dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
            } catch (Exception exception) {
                if (observer != null) {
                    observer.dispatchingThrewException(token, msg, exception);
                }
                throw exception;
            } finally {
                ThreadLocalWorkSource.restore(origWorkSource);
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            if (logSlowDelivery) {
                if (slowDeliveryDetected) {
                    if ((dispatchStart - msg.when) <= 10) {
                        Slog.w(TAG, "Drained");
                        slowDeliveryDetected = false;
                    }
                } else {
                    if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
                            msg)) {
                        // Once we write a slow delivery log, suppress until the queue drains.
                        slowDeliveryDetected = true;
                    }
                }
            }
            if (logSlowDispatch) {
                showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", 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 的对象通过Looper.prepare()创建,MessageQueue的对象通过Looper的构造方法创建。
Looper随后调用loop()方法开启死循环,loop()方法会通过MessageQueue对象提供的next()方法找到下一个需要被分发的消息,在调用msg.target.dispatchMessage(msg)把消息分发出去。而handler.sendMessage(msg)其实是把消息,加入到MessageQueue 队列,并且通知loop()继续循环,进而达到消息分发的目的

这里其实还有一个问题,就是loop()循环是死循环为什么不会导致应用卡死?
其实在没有消息的时候,MessageQueue的next()方法中会通过 nativePollOnce(ptr, nextPollTimeoutMillis)方法堵塞,不占用资源,MessageQueue 中enqueueMessage()方法中会通过nativeWake(mPtr)唤醒next()方法继续执行。
Android中为什么主线程不会因为Looper.loop()里的死循环卡死?

到目前为止Handler分发消息的流程算是搞明白了,那它是怎么做到跨线程通信的那?(线程是作何做到隔离的)
这得益于Looper中的ThreadLocal,它其实就是一个数据结构,其内部引用Thread的ThreadLocalMap负责存储指定键值对数据,key就是当前线程,value是要被存储的数据对象。通过这么一个数据结构就能保证一个线程绑定一个对应的Looper,MessageQueue,在一个线程通过ThreadLocal.get()到的Looper一定是属于这个线程的。
说的优点绕,其实就是Looper.prepare()方法在哪个线程调用,就会为该线程创建Looper ,loop()方法调用就会在该线程开启消息循环。ThreadLocal做到了线程隔离的作用。

前面说了到ThreadLocalMap 是ThreadLocal中真正用于存储当前线程信息和数据的真正对象,而ThreadLocalMap创建后,是被Thread类持有

//ThreadLocal类中createMap()
void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

由此可以看出Thread类持有一个ThreadLocalMap的引用,这个数据结构的作用就是以当前线程为Key来存储数据(value)。
我们简单梳理一下:
在每一个Thread中只要使用到了ThreadLocal对象,就会创建ThreadLocalMap,ThreadLocalMap被Thread持有引用,每次ThreadLocal.set(value)就会把对应的值存放到Thread中的ThreadLocalMap中,每次ThreadLocal.get()就会获取以当前线程为key存入的值(value)进而实现了线程的隔离,隔离的目的是保证了每个线程只有一个Value(这里就是Looper)实例,进而节省内存开销。

IdleHandler如何使用?
IdleHandler是什么?

    public static interface IdleHandler {
        /**
         * 用于消息被执行完,阻塞时调用,(空闲时)
         * 返回ture ,下次(空闲时)继续调用,返回false 只调用当次,调用完成移除,下次不再调用
         */
        boolean queueIdle();
    }

可以看到IdleHandler是在消息队列中没有消息时调用,我们知道Activity的生命周期,用户的点击,滑动等操作都是Handler发送消息贯穿,这个IdleHandler的调用时机可以认为是现在APP无任务执行,用户也没有操作就会回调这个接口的方法。

Looper.myQueue().addIdleHandler {
            //(主线程中)执行不耗时操作
            false
        }

那么它有没有什么具体的使用场景那?
可以用于初始化一下Application中onCreate()方法中不那么紧急需要初始化的第三方库,可以优化启动速度。可以做用户引导等。

Handler发送的Message有没有什么讲究?
Message分类

  • 同步消息(普通消息)
  • 屏障消息 (可以理解为一种标记消息)
  • 异步消息 (可以晋升为高优先级的被标记过的消息)

异步消息:msg.setAsynchronous(true)消息实体调用该方法后变为异步消息。
屏障消息:msg.traget==null的消息为屏障消息,消息实体并没有与之绑定的handler对象(不用被分发的消息)。
同步消息:msg.setAsynchronous(false) 为普通消息,创建的消息默认为同步消息。

屏障消息作用:可以指定屏障消息发送到消息队列,消息队列的next(),在遇到屏障消息时,会优先取出异步消息,直到异步消息被分发完后,进入阻塞。直到MessageQueue的removeSyncBarrier(token)的方法被调用,移除屏障消息后,普通消息才能正常分发。

val handler = Handler()
//插入屏障消息
val token: Int = Looper.myQueue().postSyncBarrier()
//doSomething 例如发送异步消息
val msg = Message()
msg.isAsynchronous = true //将此消息设置为异步消息
handler.sendMessage(msg) //再有屏障消息的前提下,这个消息会优先与其他普通消息被分发

// ...

//移除屏障消息
Looper.myQueue().removeSyncBarrier(token)

那么屏障消息时如何做到这种效果的那?我们看下MessageQueue的next()源码

 Message next() {
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; //  仅在第一次迭代会出现-1
        int nextPollTimeoutMillis = 0; //0 立即唤醒线程,    > 0 延迟多久唤醒线程,   -1无限阻塞线程
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }

            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // 尝试检索下一条消息。如果找到则返回
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;//队头消息
                //如果队头消息是屏障消息 (可见这个消息不被移除,普通消息将永无出头之日)
                if (msg != null && msg.target == null) {//符合这个条件的消息就是屏障消息
                    // 被屏障消息挡住了,在队列中查找下一个异步消息
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());//必须是异步消息
                }
                if (msg != null) {
                    if (now < msg.when) {
                        //下一条消息尚未准备好,设置唤醒时机
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                    } else {
                        //获取一个消息
                        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 {
                    // 没有消息了,无限延迟
                    nextPollTimeoutMillis = -1;
                }

                // 所有挂起的消息都是已处理,请处理退出消息
                if (mQuitting) {
                    dispose();
                    return null;
                }

                // IdleHandles 的回调仅在消息队列为空或第一条消息时运行
                if (pendingIdleHandlerCount < 0
                        && (mMessages == null || now < mMessages.when)) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount <= 0) {
                    // 没有IdleHandles 需要处理,阻塞控制位=true
                    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;

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf(TAG, "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // 将空闲处理程序计数重置为0,这样我们就不再运行它们
            pendingIdleHandlerCount = 0;
            //不需要延迟
            nextPollTimeoutMillis = 0;
        }
    }

这段代码不仅说明了屏障消息工作机制,也明确了IdleHandler确实会在空闲(没有msg被处理时)时被调用。

那么屏障消息有什么作用那?
其实也可以看到,它能提升异步消息的优先级,在View绘制过程中就是使用异步消息+屏障消息,让视图的呈现优先级高于其他普通消息,保证了流畅性。

好了说了那么多,总结一下:

Handler机制的流程:App程序被加载时会创建主线程的Looper对象(Looper.prepare()),Looper构造方法会创建MessageQueue,随后加载Appliction,Activity等,最后开启Looper循环(Looper.loop()),之后通过sendMessage()发送的消息会被加入到消息队列,loop()循环会从消息队列一直轮询,通过MessageQueue的next()找到需要被分发的消息,再由msg.traget.dispacthMessage(msg)把消息分发给handler的方法。

Handler怎么做到线程隔离:通过ThreadLocal实现线程的隔离,线程隔离的目的就是保证每个线程只有一个Looper。至于handler跨线程通信就是利用了不同线程共享变量的方式,无论handler在哪里发送消息,消息的处理总是被送到创建Handler,创建Looper的线程处理。

IdleHandler的使用:IdleHandler接口在消息队列空闲时被调用,可以用来处理不那么紧急的任务。

消息有什么讲究:消息分为普通,屏障,异步三类消息。屏障消息入队,消息队列在处理到屏障消息时,就会把它之后所有的异步消息依次优先处理,直到屏障消息被移除,普通消息才能依次分发。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值