美文网首页
Android:Handler总结

Android:Handler总结

作者: 我在等你回复可你没回 | 来源:发表于2018-08-07 21:14 被阅读23次

handler 总结

handler架构包含那几个实体

handler:插入消息到messagequeue
looper:从messagequeue中去消息
messagequeue:存储消息,链表结构

如何保证每个线程有一个looper

使用ThreadLocal<Looper>

如何唤醒messagequeue

插入消息的时候去唤醒它,底层使用epoll,往epoll写入消息唤醒他。

空闲消息处理器和同步屏障机制听过吗?

都是优先级相关,同步屏障机制优先级最高,空闲消息处理器优先级最低。
https://blog.csdn.net/asdgbc/article/details/79148180
https://blog.csdn.net/kc58236582/article/details/52919904

分析一下去消息的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);  //第一次取消息会马上返回,因为nextPollTimeoutMillis=0

            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) {   //如果没有target,就优先处理
                    // 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) {    //消息还没到,再等等,这个等待时间会传到epoll
                        // 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.
               //处理空闲handler
                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;   //没有消息,也没有空闲的handler,block住,mBlocked为true的话,插入message就会唤醒epoll
                    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.
            //处理空闲handler
            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.
             //能到这里,说明处理了空闲handler,下次nativepollonce也不需要等待,直接返回
            nextPollTimeoutMillis = 0;
        }
    }

解析一些epoll函数

int epoll_create(int size) 创建epoll
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
第一个参数是epoll_create返回值,第二个参数代表操作,第三个参数代表监听的fd,第四个参数是告诉内核需要监听什么事
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)

等待事件的产生,类似于select()调用。参数events用来从内核得到事件的集合,maxevents表示每次能处理的最大事件数,告之内核这个events有多大,这个maxevents的值不能大于创建epoll_create()时的size,参数timeout是超时时间(毫秒,0会立即返回,-1将不确定,也有说法说是永久阻塞)。该函数返回需要处理的事件数目,如返回0表示已超时。

handler里面都是怎样的
epoll_create:
mEpollFd = epoll_create(EPOLL_SIZE_HINT);

epoll_ctl:
eventItem.events = EPOLLIN;
eventItem.data.fd = mWakeEventFd;
int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);

epoll_wait:
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);

native looper机制是怎样的?

通过addfd来实现的。也是通过epoll来实现等待,可以有callback。
https://blog.csdn.net/chwan_gogogo/article/details/46953563

相关文章

  • Handler

    Handler机制详细解析请参考参考android中handler的一些总结以及使用(一)之handler的基本用...

  • (转载自diycode)2017 Android 面试题分享整理

    Android(安卓) Android基础知识 Android内存泄漏总结 Handler内存泄漏分析及解决 An...

  • Android 基础

    1、Android布局 2、Android内存泄漏总结 3 、Handler内存泄漏分析及解决 4...

  • 我要做 Android 之消息机制

    Android的消息机制指的是Handler的运行机制,本篇将总结Handler机制的相关知识点: 消息机制概述 ...

  • 我要做 Android 之消息机制

    Android的消息机制指的是Handler的运行机制,本篇将总结Handler机制的相关知识点: 消息机制概述 ...

  • 要点提炼|开发艺术之消息机制

    Android的消息机制指的是Handler的运行机制,本篇将总结Handler机制的相关知识点: 消息机制概述 ...

  • android学习资料

    第一部分: Android(安卓) Android基础知识 Android内存泄漏总结 Handler内存泄漏分析...

  • Android:Handler总结

    handler 总结 handler架构包含那几个实体 handler:插入消息到messagequeueloop...

  • Android Handler总结

    Handler是什么 Handler是Android提供的用来更新UI的一套机制, 也是一套消息处理机制,我们可以...

  • Android Handler 总结

    基本概念 Handler Handler 主要用于异步消息的处理。 与 Looper 沟通,Push 新消息到 M...

网友评论

      本文标题:Android:Handler总结

      本文链接:https://www.haomeiwen.com/subject/cnhzvftx.html