handler 消息处理机制

关于handler消息处理机制,只要一提到,相信作为一个android工程师,脑海就会有这么一个流程

这里写图片描述

大家都滚瓜烂熟了,但别人问到几个问题,很多人还是栽到这个“烂”上面,比如:

  • 一个线程是如何对应一个Lopper的?
  • messageQueue是如何做到线程安全的?
    首先先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));}

这里先通过sThreadLocal.get()去获取这个Looper, 获取不到再去通过new (Looper(quitAllowed))去创建Looper()对象。再来看看这个构造函数:

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

构造函数私有的,只能通过Prepare去初始化,这里就形成了一个关系,一个Looper 对应一个MessageQueue。

回过头再来分析,线程是如何和Looper对应的。

/*** Return the Looper object associated with the current thread.  Returns* null if the calling thread is not associated with a Looper.*/public static @Nullable Looper myLooper() {return sThreadLocal.get();}

在Prepare()的时候给sThreadLocal.set(Looper), 获取Looper的对象是通过sThreadLocal.get()返回的,下面看看sThreadLocal:

 // sThreadLocal.get() will return null unless you've called prepare().static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

这是一个静态成员变量所有的Looper对象共有属性。
sTreadLocal保存线程的唯一值,这样就保证一个线程对应一个looper.

至于MessageQueue的线程同步,会有奇葩的人问到,无非就是syncnized关键字,或者加锁。

boolean enqueueMessage(Message msg, long when) {if (msg.target == null) {throw new IllegalArgumentException("Message must have a target.");}if (msg.isInUse()) {throw new IllegalStateException(msg + " This message is already in use.");}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) {// 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.nextprev.next = msg;}// We can assume mPtr != 0 because mQuitting is false.if (needWake) {nativeWake(mPtr);}}return true;}

这里用的是一个加锁的方式。
在Loop.loop中有一死循环,那么当没有消息时,主线程不可能死掉吧?
“`
在主线程的MessageQueue没有消息时,便阻塞在loop的queue.next()中的nativePollOnce()方法里,此时主线程会释放CPU资源进入休眠状态,直到下个消息到达或者有事务发生,通过往pipe管道写端写入数据来唤醒主线程工作。这里采用的epoll机制,是一种IO多路复用机制,可以同时监控多个描述符,当某个描述符就绪(读或写就绪),则立刻通知相应程序进行读或写操作,本质同步I/O,即读写是阻塞的。 所以说,主线程大多数时候都是处于休眠状态,并不会消耗大量CPU资源。

还有什么关于handler的问题,希望大家可以提出来。

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

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

相关文章

es6简单介绍

let和const 原先声明变量的形式 var test 5; //全局变量 function a() {var cc3; //局部变量alert(test); } function b(){alert(test);}test 5;//全局变量 function a() {aa3; //全局变量alert(test); } 在es6之前&#xff0c;作用域只有全局作用域和函数作用域&#xff0…

软件工程方法学要素含义_日期时间数据的要素工程

软件工程方法学要素含义According to Wikipedia, feature engineering refers to the process of using domain knowledge to extract features from raw data via data mining techniques. These features can then be used to improve the performance of machine learning a…

洛谷P1605:迷宫(DFS)

题目背景 迷宫 【问题描述】 给定一个N*M方格的迷宫&#xff0c;迷宫里有T处障碍&#xff0c;障碍处不可通过。给定起点坐标和终点坐标&#xff0c;问: 每个方格最多经过1次&#xff0c;有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式&#xff0c;每次只…

vue图片压缩不失真_图片压缩会失真?快试试这几个无损压缩神器。

前端通常在做网页的时候 会出现图片加载慢的情况 在这里我通常会将图片进行压缩 但是通常情况下 观众会认为 图片压缩会出现失真的现象 在这里我会向大家推荐几款图片压缩的工具 基本上会实现无损压缩1.TinyPng地址&#xff1a;https://tinypng.comEnglish&#xff1f;不要慌&a…

remoteing2

此示例主要演示了net remoting,其中包含一个服务器程序Server.exe和一个客户端程序CAOClient.exe。客户端程序会通过http channel调用服务器端RemoteType.dll的对象和方法。服务器端的代码文件由下图所述&#xff1a;Server.cs源代码 :using System;using System.Runtime.Remot…

android 线程池

为什么用线程池 创建/销毁线程伴随着系统开销&#xff0c;过于频繁的创建/销毁线程&#xff0c;会很大程度上影响处理效率 例如&#xff1a; 记创建线程消耗时间T1&#xff0c;执行任务消耗时间T2&#xff0c;销毁线程消耗时间T3 如果T1T3>T2&#xff0c;那么是不是说开…

datatable转化泛型

public class ConvertHelper<T>where T:new() { /// <summary> /// 利用反射和泛型 /// </summary> /// <param name"dt"></param> /// <returns></returns> public static List<T> ConvertToList(DataTable dt) { …

【跃迁之路】【651天】程序员高效学习方法论探索系列(实验阶段408-2018.11.24)...

(收集箱&#xff08;每日一记&#xff0c;每周六整理&#xff09;)专栏 实验说明 从2017.10.6起&#xff0c;开启这个系列&#xff0c;目标只有一个&#xff1a;探索新的学习方法&#xff0c;实现跃迁式成长实验期2年&#xff08;2017.10.06 - 2019.10.06&#xff09;我将以自己…

更换mysql_Docker搭建MySQL主从复制

Docker搭建MySQL主从复制 主从服务器上分别安装Docker 1.1 Docker 要求 CentOS 系统的内核版本高于 3.10 [rootlocalhost ~]# uname -r 3.10.0-693.el7.x86_641.2 确保 yum 包更新到最新。 [rootlocalhost ~]# sudo yum update Loaded plugins: fastestmirror, langpacks Loadi…

dll文件的c++制作dll文件的c++制作

dll文件的c制作1、首先用vs2005建立一个c的dll动态链接库文件&#xff0c;这时&#xff0c;// DllTest.cpp : 定义 DLL 应用程序的入口点。//#include "stdafx.h"//#include "DllTest.h"#ifdef _MANAGED#pragma managed(push, off)#endifBOOL APIENTRY Dll…

理解ConstraintLayout 对性能的好处

自从在17年GoogleI/O大会宣布了Constraintlayout,我们持续提升了布局的稳定性和布局编辑的支持。我们还为ConstraintLayout添加了一些新特性支持创建不同类型的布局&#xff0c;添加这些新特性&#xff0c;可以明显的提升性能&#xff0c;在这里&#xff0c;我门将讨论Contrain…

数据湖 data lake_在Data Lake中高效更新TB级数据的模式

数据湖 data lakeGOAL: This post discusses SQL “UPDATE” statement equivalent for a data lake (object) storage using Apache Spark execution engine. To further clarify consider this, when you need to perform conditional updates to a massive table in a relat…

如何理解运维

运维工程师&#xff08;运营&#xff09;&#xff0c;负责维护并确保整个服务的高可用性&#xff0c;同时不断优化系统架构提升部署效率&#xff0c;优化资源利用率提高整体的投资回报率。运维工程师面对的最大挑战是大规模集群的管理问题&#xff0c;如何管理好几十万台服务器…

advanced installer更换程序id_好程序员web前端培训分享kbone高级-事件系统

好程序员web前端培训分享kbone高级-事件系统&#xff1a;1、用法&#xff0c;对于多页面的应用&#xff0c;在 Web 端可以直接通过 a 标签或者 location 对象进行跳转&#xff0c;但是在小程序中则行不通&#xff1b;同时 Web 端的页面 url 实现和小程序页面路由也是完全不一样…

ai对话机器人实现方案_显然地引入了AI —无代码机器学习解决方案

ai对话机器人实现方案A couple of folks from Obviously.ai contacted me a few days back to introduce their service — a completely no-code machine learning automation tool. I was a bit skeptical at first, as I always am with supposedly fully-automated solutio…

网络负载平衡的

网络负载平衡允许你将传入的请求传播到最多达32台的服务器上&#xff0c;即可以使用最多32台服务器共同分担对外的网络请求服务。网络负载平衡技术保证即使是在负载很重的情况下它们也能作出快速响应。 网络负载平衡对外只须提供一个IP地址&#xff08;或域名&#xff09;。 如…

透明状态栏导致windowSoftInputMode:adjustResize失效问题

当我们通过下面代码&#xff1a; getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 设置状态栏透明&#xff0c;当界面存在EditText时&#xff0c;在activity里面设置windowSoftInputMode:…

[TimLinux] JavaScript 元素动态显示

1. css的opacity属性 这个属性用于&#xff1a;设置元素的不透明级别&#xff0c;取值范围&#xff1a;从 0.0 &#xff08;完全透明&#xff09;到 1.0&#xff08;完全不透明&#xff09;&#xff0c;元素所在的文本流还在。这个属性的动态变化可以用来设置元素的淡入淡出效果…

神经网络 CNN

# encodingutf-8import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_datamnist input_data.read_data_sets(MNIST_data, one_hotTrue)def weight_variable(shape): initial tf.truncated_normal(shape, stddev0.1) # 定义…

图片中的暖色或冷色滤色片是否会带来更多点击? —机器学习A / B测试

A/B test on ads is the art of choosing the best advertisement that optimizes your goal (number of clicks, likes, etc). For example, if you change a simple thing like a filter in your pictures you will drive more traffic to your links.广告的A / B测试是一种选…