Android Looper简介

news2024/9/21 0:49:04

本文基于安卓11。

Looper是一个用具,在安卓程序中,UI线程是由事件驱动的(onCreate, onResume, onDestory),Looper就是处理这些事件的工具,事件被描述为消息(Message),Looper维护一个按时间顺序排列的消息队列(MessageQueue),在要求的时间将消息交给Hanlder处理。

一、消息循环

默认情况下一个线程不存在消息循环,Android线程开启消息循环需要调用Looper.prepare(),Looper.loop()。

二、Looper.prepare()

初始化Looper和MessageQueue对象,存在应用层的Java对象和Native层的cpp对象。

//Looper.java  
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();  
	   private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

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

可以看到Looper对象通过ThreadLocal获取,被设计为线程单例。

//MessageQueue.java 
		MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        mPtr = nativeInit();
    }

MessageQueue构造方法通过nativeInit()初始化native层的消息循环,创建native层的Looper,NativeMessageQueue对象,mPtr是NativeMessageQueue对象引用,后续MessageQueue(Java)调用nativePollOnce(long ptr, int timeoutMillis), nativeWake(long ptr)等native函数都需要传入这个参数,NativeMessageQueue可使用此参数通过JNIEnv*调用Java方法。

2.1 nativeInit()
//android_os_MessageQueue.cpp
static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) {
  NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue();
  nativeMessageQueue->incStrong(env);
  return reinterpret_cast<jlong>(NativeMessageQueue);
}
NativeMessageQueue::NativeMessageQueue() :
        mPollEnv(NULL), mPollObj(NULL), mExceptionObj(NULL) {
    mLooper = Looper::getForThread();
    if (mLooper == NULL) {
        mLooper = new Looper(false);
        Looper::setForThread(mLooper);
    }
}

mPollEnv,mPollObj都置为NULL,获取当前线程Looper,返回NativeMessageQueue对象引用给Java层。

//Looper.cpp
Looper::Looper(bool allowNonCallbacks)
    : mAllowNonCallbacks(allowNonCallbacks),
      mSendingMessage(false),
      mPolling(false),
      mEpollRebuildRequired(false),
      mNextRequestSeq(WAKE_EVENT_FD_SEQ + 1),
      mResponseIndex(0),
      mNextMessageUptime(LLONG_MAX) {
    mWakeEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
    LOG_ALWAYS_FATAL_IF(mWakeEventFd.get() < 0, "Could not make wake event fd: %s", strerror(errno));

    AutoMutex _l(mLock);
    rebuildEpollLocked();
}

重置mWakeEventFd,eventfd系统调用创建一个匿名文件描述符,rebuildEpollLocked()重置epoll事件。

//Looper.cpp
void Looper::rebuildEpollLocked() {
    // Close old epoll instance if we have one.
    if (mEpollFd >= 0) {
#if DEBUG_CALLBACKS
        ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
#endif
        mEpollFd.reset();
    }

    // Allocate the new epoll instance and register the WakeEventFd.
    mEpollFd.reset(epoll_create1(EPOLL_CLOEXEC));
    LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));

    epoll_event wakeEvent = createEpollEvent(EPOLLIN, WAKE_EVENT_FD_SEQ);
    int result = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mWakeEventFd.get(), &wakeEvent);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s",
                        strerror(errno));

    for (const auto& [seq, request] : mRequests) {
        epoll_event eventItem = createEpollEvent(request.getEpollEvents(), seq);

        int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, request.fd, &eventItem);
        if (epollResult < 0) {
            ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s",
                  request.fd, strerror(errno));
        }
    }
}

epoll_create1()创建新的epoll实例,将唤醒事件mWakeEventFd添加到epoll实例,后面可以看到调用Looper::wake()方法在mWakeEventFd写值唤醒epoll_wait()。

三、Looper.loop()

//Looper.java
public static void loop() {
    final Looper me = myLooper();
  //...
		for (;;) {
        if (!loopOnce(me, ident, thresholdOverride)) {
            return;
        }
    }
}

private static boolean loopOnce(final Looper me,
            final long ident, final int thresholdOverride) {
    Message msg = me.mQueue.next(); // might block
    if (msg == null) {
        // No message indicates that the message queue is quitting.
        return false;
    }
    //...
    try {
        msg.target.dispatchMessage(msg);
      //...
    }
    //...
    msg.recycleUnchecked();
    return true;
}

mQueue为Looper类的MessageQueue对象,loop方法很简单,循环获取处理mQueue的Message。

如果msgnull时返回false,loop退出消息循环,这种情况只会在mPtr0(nativeInit初始化失败),或者Lopper.quit()时才会出现,messageQueue处于空闲时会一直阻塞等待笑一个msg,而不会返回null。

msg.target是Handler对象,成功获取到msg后就会调用到handler的dispatchMessage(msg)---->handleMessage(msg)方法了。

msg.recycleUnchecked()对创建的Message对象进行缓存回收,避免创建过多的Message对象。

3.1 MessageQueue.next()
//MessageQueue.java
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;
        }
    }

重点关注nextPollTimeoutMillis,mBlocked两个变量:

  • nextPollTimeoutMillis:epoll阻塞时间。
  • mBlocked:当前线程是否处于阻塞状态。

当没有需要处理的消息时,msg==null,nextPollTimeoutMillis值为-1,否则为下个需要处理消息的时间。

3.1.1 返回msg,交给handler处理

next()维护一个按时间排序的Message队列,如果有需要处理的消息,msg不为空(msg != null),且当前时间小于msg.when(now < msg.when),则返回当前的msg,交给handler处理。

3.1.2 处理IdleHandler
private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>();

mIdlehandler变量保存着IdleHandler对象,当暂时没有需要处理的消息时,检查mIdleHandlers.size()是否为0,是否有需要处理的IdleHandler,利用空闲时间处理,可以看出IdleHandler处理时间不可控。关于IdleHandler(https://blog.csdn.net/jdsjlzx/article/details/110532500)

3.1.3 nativePollOnce

没有消息需要处理,也没有IdleHandler需要处理,nativePollOnce(ptr, nextPollTimeoutMillis);使当前线程阻塞,进入休眠状态。

参数timeoutMillis就是Java应用层传递过来的nextPollTimeoutMillis变量。

//Looper.cpp
int Looper::pollInner(int timeoutMillis) {
    //...
  
    // We are about to idle.
    mPolling = true;

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

    // No longer idling.
    mPolling = false;
  //...
    return result;
}

epoll_wait()系统调用等待文件描述符mEpollFd引用的epoll实例上的事件,从此线程进入休眠状态,直到timeoutMillis时间结束,或者接收到新的事件。

四、发送消息

Handler通过Looper.myLooper()获取当前线程的Looper对象,通过Looper对象获取MessageQueue对象mQueue=mLooper.mQueue。

sendEmptyMessage(int what)最终调用MessageQueue的enqueueMessage()方法

//MessageQueue.java
boolean enqueueMessage(Message msg, long when) {
//...
        synchronized (this) {

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

msg.when表示Message应该处理的时间,按照时间顺序维护Message列表。通过mBlocked变量判断当前线程是否阻塞,如果已经阻塞且消息不是异步消息,调用nativeWake(mPtr)方法唤醒当前线程。

4.1 nativeWake
//Looper.cpp
void Looper::wake() {
    uint64_t inc = 1;
    ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd.get(), &inc, sizeof(uint64_t)));
    if (nWrite != sizeof(uint64_t)) {
        if (errno != EAGAIN) {
            LOG_ALWAYS_FATAL("Could not write wake signal to fd %d (returned %zd): %s",
                             mWakeEventFd.get(), nWrite, strerror(errno));
        }
    }
}

通过往mWakeEventFd描述符写值1,唤醒3.1.3 nativePollOnce节中Looper::pollInner()方法的系统调用epoll_wait(),结束阻塞状态,开始处理消息。

类关系图:

在这里插入图片描述

参考链接:

nativePollOnce(); (https://stackoverflow.com/questions/38818642/android-what-is-message-queue-native-poll-once-in-android)

IdleHandler (https://blog.csdn.net/jdsjlzx/article/details/110532500)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/393453.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【PHP代码注入】PHP代码注入漏洞

漏洞原理RCE为两种漏洞的缩写&#xff0c;分别为Remote Command/Code Execute&#xff0c;远程命令/代码执行PHP代码注入也叫PHP代码执行(Code Execute)(Web方面)&#xff0c;是指应用程序过滤不严&#xff0c;用户可以通过HTTP请求将代码注入到应用中执行。代码注入(代码执行)…

python甜橙歌曲音乐网站平台源码

wx供重浩&#xff1a;创享日记 对话框发送&#xff1a;python音乐 获取完整源码源文件说明文档配置教程等 在虚拟环境下输入命令“python manage.py runserver”启动项目&#xff0c;启动成功后&#xff0c;访问“http://127.0.0.1:5000”进入甜橙音乐网首页&#xff0c;如图1所…

YOLOS调试记录

YOLOS是由华中科大提出的将Transformer迁移到计算机视觉领域的目标检测方法&#xff0c;其直接魔改ViT&#xff01;本文首次证明&#xff0c;通过将一系列固定大小的非重叠图像块作为输入&#xff0c;可以以纯序列到序列的方式实现2D目标检测。 模型结构 下面来调试一下该项目…

【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)

&#x1f48c; 所属专栏&#xff1a;【微信小程序开发教程】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &…

javaScript基础面试题 ---对象考点

1、对象是通过new操作符构建出来的&#xff0c;所以对象之间不相等 2、对象注意引用类型&#xff0c;如果是引用类型&#xff0c;就可能会相等 3、现在对象本身查找 -> 构造函数中找 -> 对象原型中找 -> 构造函数原型中找 -> 对象上一层原型… 1、对象是通过new操作…

被骗进一个很隐蔽的外包公司,入职一个月才发现,已经有了社保记录,简历污了,以后面试有影响吗?...

职场的套路防不胜防&#xff0c;一不留神就会掉坑&#xff0c;一位网友就被“骗”进了外包公司&#xff0c;他说公司非常隐蔽&#xff0c;入职一个月才发现是外包&#xff0c;但已经有了社保记录&#xff0c;简历污了&#xff0c;不知道对以后面试有影响吗&#xff1f;楼主说&a…

Mysql迁移Postgresql

目录原理环境准备操作系统(Centos7)Mysql客户端安装Psql客户端安装数据库用户导表脚本dbmysql2pgmysqlcopy测试在mysql中建表导表测试查看pg中的表原理 Mysql抽取&#xff1a;mysql命令重定向到操作系统文件&#xff0c;处理成csv文件&#xff1b; PG装载&#xff1a;copy方式…

【大数据源码】Hadoop源码解读 Namenode 启动加载FsImage的过程

Namenode 启动前言启动 Namenode 组件启动脚本Namenode.initializeFSNamesystem.loadFromDiskFsImage.recoverTransitionReadFSImageFormat.loadFSImageFormatProtobuf.load反序列化加载FsImage文件内容FsImage内存数据结构前言 NameNode是HDFS中负责元数据管理的组件&#xf…

PhpStudy下载安装使用教程,图文教程(超详细)

「作者简介」&#xff1a;CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」&#xff1a;对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 PhpStudy一、官网下载二、安装三、简单使用四、粉丝福利PhpStudy&#xff1a;让天下没有难…

bable和AST概述

这里写目录标题bable定义Babel概述Babel 中重要的对象VistorAST定义Javascript 语法的AST&#xff08;抽象语法树&#xff09;bable 定义 Babel 是我们知道的将 ES6、ES7等代码转译为 ES5 代码且能安全稳定运行最好的工具同时它允许开发者开发插件&#xff0c;能够在编译时期…

关于 interface{} 会有啥注意事项?上

学习 golang &#xff0c;对于 interface{} 接口类型&#xff0c;我们一定绕不过&#xff0c;咱们一起来看看 使用 interface{} 的时候&#xff0c;都有哪些注意事项吧 interface {} 可以用于模拟多态 xdm 咱们写一个简单的例子&#xff0c;就举动物的例子 写一个 Animal 的…

【LeetCode】剑指 Offer(17)

目录 题目&#xff1a;剑指 Offer 34. 二叉树中和为某一值的路径 - 力扣&#xff08;Leetcode&#xff09; 题目的接口&#xff1a; 解题思路&#xff1a; 代码&#xff1a; 过啦&#xff01;&#xff01;&#xff01; 写在最后&#xff1a; 题目&#xff1a;剑指 Offer …

Spring Cache简单介绍和使用

目录 一、简介 二、使用默认ConcurrentMapManager &#xff08;一&#xff09;创建数据库和表 &#xff08;二&#xff09;创建boot项目 &#xff08;三&#xff09;使用Api 1、EnableCaching 2、CachePut 3、cacheable 4、CacheEvict 三、使用redis作为cache 一、简…

云计算基础——云计算认知

云计算的总体框架在服务方面&#xff0c;主要以提供用户基于云的各种服务为主&#xff0c;共包含3个层次:1.软件即服务&#xff08;Software as a Service&#xff0c;简称SaaS)&#xff0c;这层的作用是将应用主要以基于Web 的方式提供给客户;2.平台即服务(Platform as a Serv…

STL讲解——模拟实现vector

STL讲解——模拟实现vector vector深度剖析 在STL源码中&#xff0c;发现vector定义的并不是 start、size、capacity&#xff0c;而是start、finish、end_of_storage. 这样就可以得到size()和capacity()。 sizefinish-start capacityend_of_storage-start 扩容可能是本地扩容也…

Entity Framework简单使用

我喜欢比较老派的database first &#xff0c; 所以先创建sql server的数据库&#xff0c;比如dbname叫做&#xff1a;Blogging这里我省略了。 在visual studio里面创建一个控制台程序&#xff0c; 然后添加ado.net项目 选择“gen from database” 然后新建你的数据库连接&…

基于粒子群优化算法的分布式电源选址定容【IEEE33节点】(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5;&#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密…

测试人员须要知道的性能测试那些事

随着各企业的业务发展、用户量以及数据量的不断增加&#xff0c;系统承载的压力也会随之增加&#xff0c;服务系统的性能好坏又严重影响企业的利益。因此&#xff0c;性能测试重要性与需求越来越强烈。 常见的性能测试目的 性能测试是确定系统在特定工作负载下的稳定性和响应…

数智引航 共赢未来 | 科士达2023数据中心合作伙伴大会圆满召开!

3月5日&#xff0c;科士达2023年全国合作伙伴大会在广东深圳隆重召开&#xff0c;本次大会以“数智引航 共赢未来”为主题&#xff0c;来自全国各地的核心渠道伙伴齐聚一堂&#xff0c;深度交流&#xff0c;展望未来&#xff0c;共同探讨数据中心技术发展趋势&#xff0c;碰撞…

[Python图像处理] 使用高通滤波器实现同态滤波

使用高通滤波器实现同态滤波同态滤波基础实现同态滤波相关链接同态滤波基础 同态滤波是一种去除图像中乘性噪声的技术&#xff0c;常用于校正图像中的不均匀照明。根据图像形成的光照反射模型&#xff0c;图像 f(x,y)f(x,y)f(x,y) 可以由以下两个分量表征&#xff1a; 入射到…