Handler机制

Handler机制_第1张图片
消息机制

Java层

1.Looper构建了消息队列MessageQueue
2.Message的成员target关联Handler
3.Handler的成员mQueue关联MessageQueue,成员mLooper关联Looper
4.MessageQueue的成员mMessages关联Message

我们使用Handler时都是先要调Looper的prepare方法,用于创建Looper和MessageQueue,主线程直接使用Handler是因为在系统已经提前给我们创建好了主线程的Looper和MessageQueue
创建App进程后会调用ActivityThread的main()方法

public static void main(String[] args) {
        ···
        Looper.prepareMainLooper();
        ····
        Looper.loop();
    }

1,创建主线程Looper
2,为主线程Handler赋值
3,Looper.looper启动循环

Looper的prepare()

Looper.prepareMainLooper

   public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }

prepare(false)这里的prepare传的是false,我们自己创建的线程调用Looper.prepare()传递的是true表示可以退出
sMainLooper为全局主线程Looper赋值

public static void prepare() {
    prepare(true);
}
private static void prepare(boolean quitAllowed) {
    if (sThreadLocal.get() != null) {  /* 如果消费者线程已有Looper绑定了,则抛出异常 */
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    sThreadLocal.set(new Looper(quitAllowed)); /* 创建Looper,并绑定到消费者线程 */
}

如果Looper.prepare只能调用一次,如果多次调用会抛异常
Looper创建后会存储在ThreadLocal中,ThreadLocal可以保证线程是唯一的

Looper的构造函数

private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed); //创建消息队列
    mThread = Thread.currentThread();       //记录消费者线程
}

Looper的loop()

public static void loop() {
......
    for (;;) {
        Message msg = queue.next(); //消息队列出队得到Message事务
        if (msg == null) {
            return;
    }
    msg.target.dispatchMessage(msg);//执行具体事务
......
}

MessageQueue.next

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

1,nativePollOnce 如果无消息让消费者线程进入休眠状态
2,如果设置消息屏障取出异步消息
3,检测消息时间是否到达,到达取出消息,未到达设置超时时间(目标时间 - 当前时间)
4,未到达,设置超时时间,消费者线程进入休眠
5,执行IdelHanlder

msg.target.dispatchMessage(msg)

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

1,先处理Message CallBack
2,Handler的Callback
3,处理handleMessage

发送消息

Hanlder.sendMessage(),最终会调用MessageQueue的enqueueMessage

  boolean enqueueMessage(Message msg, long when) {      
          ···
            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;
    }

1,如果队列为空直接插入节点
2,队列不为空,按时间排序插入
3,线程休眠,插入消息为屏障,插入消息是异步消息并且在第一位,需要唤醒线程

Native层

Looper.prepare()

sp Looper::prepare(int opts) {
......
    /* 获取当前消费者线程线程绑定的Looper对象
     * 由于这里首次调用prepare,还未绑定Looper,因此返回空 
     */
    sp looper = Looper::getForThread();
    if (looper == NULL) {
    /* 创建Looper对象(见1.2),然后绑定到当前消费者线程中 */
        looper = new Looper(allowNonCallbacks);
        Looper::setForThread(looper);
    }

    return looper;
......
}

在Looper的构造函数
1,创建了eventfd
2,创建epoll文件描述符,用于监听eventfd
Looper.pollOnce
1,消费者线程调用epoll.wait检测是否有消息就绪,如果没有休眠
2,当fd就绪,消费者线程被唤醒

MessageQueue
构造函数中会创建Looper
 1.Looper(Java)启动消息循环,先处理Looper(Native)事务,然后再处理Looper(Java)事务
 2.Looper(Native)和Looper(Java)均无事务处理时,消费者线程会进入超时休眠状态,等待事务就绪时唤醒

参考
Android P源码分析之Looper(Native)
Android P源码分析之Handler(JAVA)

你可能感兴趣的:(Handler机制)