ActivityThread-->Android的主线程。

main方法通过Looper获取主线程、结束主线程。

H就是ActivityThread的Handler,主要包含了四大组件的启动和停止等过程。

通过ApplicationThread和AMS进行进程间通信,AMS以进程间通信的方式完成ActivityThread的请求后会回到ApplicationThread中的Binder方法,然后ApplicationThread会向H发送消息,H收到消息后会将ApplicationThread中的逻辑切换到ActivityThread中执行,即切换到主线程中去执行,这个过程就是主线程的消息循环模式。

Looper

MessageQueue

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 iterationint 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 handlerboolean 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;}}
复制代码

Message

Handler

ThreadLocal

#Hander机制

走进Java Android 的线程世界(三)Hander消息机制相关推荐

  1. 【Android】线程间通信——Handler消息机制

    文章目录 引言 Java层 永动机跑起来 示例 Looper Handler MessageQueue 永动机停下 Native层 nativeInit() nativePollOnce() nati ...

  2. 独特视角带你走进Java并发编程的世界

    独特视角带你走进Java并发编程的世界 全局概括 并发问题产生的三个根本原因 java内存模型: 有序性和可见性的解决方案 volatile关键字 Happens-Before规则 锁: 原子性的解决 ...

  3. Java 创建一个线程的三种方式

    Java 创建一个线程的三种方式 更多内容,点击了解: https://how2j.cn/k/thread/thread-start/353.html 创建多线程有3种方式,分别是继承线程类,实现Ru ...

  4. JAVA中创建线程的三种方法及比较

    JAVA中创建线程的方式有三种,各有优缺点,具体如下: 目录 一.继承Thread类来创建线程 二.实现Runnable接口来创建线程 三.通过Callable和Future来创建线程 四.三种方式创 ...

  5. Java中创建线程的三种方式

    Java中创建线程主要有三种方式: 一.继承Thread类创建线程类 (1)继承Thread类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行体. ...

  6. android异步线程利用Handler将消息发送至UI线程

    android UI线程安全 首先对于android而言,UI线程就是我们的主线程,在Android应用启动时,会自动创建一个线程,主线程负责UI的展示.UI事件消息的派发处理等等,因此主线程也叫做U ...

  7. Java微信公众平台开发(三)--接收消息的分类及实体的创建

    转自:http://www.cuiyongzhi.com/post/41.html 前面一篇有说道应用服务器和腾讯服务器是通过消息进行通讯的,并简单介绍了微信端post的消息类型,这里我们将建立消息实 ...

  8. Android 手机卫士--解析json与消息机制发送不同类型消息

    本文地址:http://www.cnblogs.com/wuyudong/p/5900800.html,转载请注明源地址. 1.解析json数据 解析json的代码很简单 JSONObject jso ...

  9. Android Framework学习(八)之Handler消息机制(Native层)解析

    在深入解析Android中Handler消息机制一文中,我们学习了Handler消息机制的java层代码,这次我们来学习Handler消息机制的native层代码. 在Java层的消息处理机制中,Me ...

最新文章

  1. 有奖评选 | 2020年的AI技术公开课,你想听到哪些干货?
  2. 分布式 id 生成器
  3. python 中有趣的库tqdm
  4. So Hot ?快给 CPU 降降温!
  5. Erasing Zeroes CodeForces - 1303A
  6. html中文本格式化、预格式化、计算机输出标签、address、title、文字方向、著作
  7. java界面布局都有什么_java入門教程-10.5Java圖形界面之布局設計
  8. mxnet img2rec的使用,生成数据文件
  9. SpringCloud分布式开发五大神兽
  10. 1.9 Important Themes(一些重要的概念)
  11. java集合拿到每个元素_java – 如何有效地小写每个元素的集合?
  12. 软件测试职业比赛,我院首届软件测试职业技能大赛圆满落幕
  13. 计算机课程哪个网校比较好,计算机软考在线学习哪个好
  14. 唯一索引(unique index)的创建和使用
  15. 宝宝纸尿裤怎么选?我联合2000位宝妈,对20款纸尿裤做了次测评
  16. Hadoop集群配置
  17. java小游戏 飞翔的小鸟,校园新手入门,分分钟带你玩转编程
  18. Python——程序设计方法学
  19. 记录:macbook 下剪切快捷键
  20. 在有Anaconda情况下,使用python命令报错的问题

热门文章

  1. 2021数据库课程设计培训笔记:【JAVA】部分
  2. 2020-03-05-stm32 学习--Stm32F407 SPI1 全双工DMA 收发数据
  3. SD卡文件损坏怎么办?SD卡数据恢复用这招
  4. jmeter辅助功能详解(http请求默认值,断言)
  5. win10使用ssh指令传输文件
  6. 布局未来移动出行,需要怎样的互联汽车解决方案?
  7. 使用oc的block方法回调
  8. flux和redux
  9. 网课python程序设计答案_Python程序设计知到网课答案
  10. 数仓建模—实时数仓架构发展史