电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android異步消息處理機(jī)制實(shí)現(xiàn)原理詳解

瀏覽:5日期:2022-09-22 13:09:23

消息處理機(jī)制主要對(duì)象:Looper,Handler,Message(還有MessageQueue和Runnable)

Looper不斷從MessageQueue消息隊(duì)列中取出一個(gè)Message,然后傳給Handle,如此循環(huán)往復(fù),如果隊(duì)列為空,那么它會(huì)進(jìn)入休眠。

這些類的主要變量

Looper.java

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); private static Looper sMainLooper; // guarded by Looper.class final MessageQueue mQueue; final Thread mThread;

Handler.java

final MessageQueue mQueue; final Looper mLooper; final Callback mCallback; final boolean mAsynchronous; IMessenger mMessenger;

Message.java

Handler target;每個(gè)消息只能對(duì)應(yīng)一個(gè)handlerRunnable callback;回調(diào)接口

MessageQueue.java

Message mMessages;

Runnable是一個(gè)空接口類,沒有變量

上一個(gè)書上的圖:

Android異步消息處理機(jī)制實(shí)現(xiàn)原理詳解

Handler和Thread沒有直接關(guān)系,但對(duì)應(yīng)關(guān)系可以推理得到

每個(gè)Thread只對(duì)應(yīng)一個(gè)Looper;

每個(gè)Looper只對(duì)應(yīng)一個(gè)MessageQueue;

每個(gè)MessageQueue對(duì)應(yīng)N個(gè)Message,每個(gè)Message只對(duì)應(yīng)一個(gè)Handler

==》每個(gè)Thread對(duì)應(yīng)N個(gè)Handler。

Handler是”真正處理事情“的地方,作用:處理消息,將Message壓入MessageQueue中

帶著一個(gè)問題看源碼:創(chuàng)建handler對(duì)象的線程(ui/主線程除外)為什么,必須先調(diào)用Looper.prepare() ?

public Handler() { this(null, false); }public Handler(Callback callback) { this(callback, false); }public Handler(Looper looper) { this(looper, null, false); } public Handler(Looper looper, Callback callback) { this(looper, callback, false); } public Handler(boolean async) { this(null, async); }public Handler(Callback callback, boolean async) { if (FIND_POTENTIAL_LEAKS) { final Class<? extends Handler> klass = getClass(); if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) {Log.w(TAG, 'The following Handler class should be static or leaks might occur: ' + klass.getCanonicalName()); } } mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException('Can’t create handler inside thread that has not called Looper.prepare()'); } mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; } public Handler(Looper looper, Callback callback, boolean async) { mLooper = looper; mQueue = looper.mQueue; mCallback = callback; mAsynchronous = async; }

初始化handler對(duì)象時(shí)(構(gòu)造方法是Handler(),Handler(Callback callback))都間接調(diào)用Handler(Callback callback, boolean async)構(gòu)造方法

主要代碼是Looper.myLooper();

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();//這是在Looper類中的定義public static Looper myLooper() { return sThreadLocal.get();//從當(dāng)前線程中獲得looper對(duì)象 }public static void prepare() {prepare(true);}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));//為當(dāng)前線程設(shè)置looper對(duì)象}

我們自己創(chuàng)建線程必須通過Looper.prepare()方法為當(dāng)前線程設(shè)置looper對(duì)象才可以通過Looper.myLooper()方法返回looper對(duì)象,這樣在非UI線程創(chuàng)建handler對(duì)象時(shí)才不會(huì)報(bào)錯(cuò)。'Can’t create handler inside thread that has not called Looper.prepare()'

ps:prepare(boolean quitAllowed)(這個(gè)不用我們關(guān)心,略過。。)

這個(gè)quitAlowed參數(shù)是定義消息隊(duì)列用了,看的源代碼是android4.4

Looper.javaprivate Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mRun = true; mThread = Thread.currentThread(); }MessageQueue.java// True if the message queue can be quit. private final boolean mQuitAllowed;//true消息隊(duì)列可以被quit,false消息隊(duì)列不能被quit。

主線程/UI線程的MessageQueue不能被銷毀掉。看源碼(銷毀調(diào)用Looper.quit())

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

偏離太遠(yuǎn)了

所以得出結(jié)論:創(chuàng)建handler對(duì)象的線程(ui/主線程除外),必須先調(diào)用Looper.prepare()

Handler作用1:處理消息

在Looper類中處理消息是通過msg.target.dispatchMessage(msg);target就是handler對(duì)象(Message類的內(nèi)部變量Handler target)將消息轉(zhuǎn)發(fā)到處理消息的對(duì)應(yīng)的handler對(duì)象上,然后這個(gè)target即handler對(duì)象會(huì)在處理消息前做一個(gè)檢查

public void dispatchMessage(Message msg) { if (msg.callback != null) {//如果msg有綁定callback回調(diào)接口Runaable不為空,則執(zhí)行Runnable的run方法 handleCallback(msg); } else { if (mCallback != null) {//如果handler的內(nèi)置接口類Callback不為空,則執(zhí)行boolean handleMessage(Message msg)這個(gè)方法if (mCallback.handleMessage(msg)) {執(zhí)行完成則return return;} } handleMessage(msg);//最后才執(zhí)行handler本身的方法 } } private static void handleCallback(Message message) { message.callback.run(); }public interface Callback {//handler的內(nèi)置接口類Callbackpublic boolean handleMessage(Message msg); }

Handler作用2:將Message壓入MessageQueue中

handler中提供的很多發(fā)送message的方法,除了sendMessageAtFrontOfQueue()方法(直接調(diào)用enqueueMessage(queue, msg, 0);)之外,其它的發(fā)送消息方法最終都會(huì)輾轉(zhuǎn)調(diào)用到sendMessageAtTime()方法

public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; if (queue == null) { RuntimeException e = new RuntimeException( this + ' sendMessageAtTime() called with no mQueue'); Log.w('Looper', e.getMessage(), e); return false; } return enqueueMessage(queue, msg, uptimeMillis); }

sendMessageAtTime()方法也是調(diào)用Handler中的enqueueMessage(queue, msg, uptimeMillis)方法

和sendMessageAtFrontOfQueue()方法兩者最后都會(huì)調(diào)用enqueueMessage(queue, msg, uptimeMillis)方法

區(qū)別是需要延遲uptimeMillis時(shí)間后才將Message壓入MessageQueue中

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this;//給msg的target賦值為handler自身然后加入MessageQueue中 if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }

最終所有的方法都是調(diào)用MessageQueue中的enqueueMessage(msg, uptimeMillis);方法,是不是感覺兩個(gè)方法差不多啊,注意參數(shù)!!

MessageQueue的使用是在Looper中

Handler的作用整理完畢(好像我現(xiàn)在已經(jīng)可以把Handler源碼完整默寫下來了。哈哈^.^記憶力真不行)

Looper類

作用:與當(dāng)前線程綁定,保證一個(gè)線程只會(huì)有一個(gè)Looper實(shí)例,同時(shí)一個(gè)Looper實(shí)例也只有一個(gè)MessageQueue。

對(duì)于Looper主要是prepare()和loop()兩個(gè)方法

prepare()將普通線程轉(zhuǎn)化為looper線程,

loop()方法,不斷從MessageQueue中去取消息,交給消息的target屬性的dispatchMessage去處理。

public static void loop() { final Looper me = myLooper(); if (me == null) { throw new RuntimeException('No Looper; Looper.prepare() wasn’t called on this thread.'); } final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); for (;;) { Message msg = queue.next(); // might block if (msg == null) {// No message indicates that the message queue is quitting.return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) {logging.println('>>>>> Dispatching to ' + msg.target + ' ' + msg.callback + ': ' + msg.what); } msg.target.dispatchMessage(msg); if (logging != null) {logging.println('<<<<< Finished to ' + msg.target + ' ' + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn’t corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) {Log.wtf(TAG, 'Thread identity changed from 0x' + Long.toHexString(ident) + ' to 0x' + Long.toHexString(newIdent) + ' while dispatching to ' + msg.target.getClass().getName() + ' ' + msg.callback + ' what=' + msg.what); } msg.recycle(); } }

27行就是上面提到了,handler進(jìn)行消息處理的關(guān)鍵代碼了

看著上面的分析很復(fù)雜,總結(jié)下

1、首先Looper.prepare()為在當(dāng)前線程中保存一個(gè)Looper實(shí)例(sThreadLocal.set()),然后該實(shí)例中保存一個(gè)MessageQueue對(duì)象;因?yàn)長(zhǎng)ooper.prepare()在一個(gè)線程中只能調(diào)用一次,所以MessageQueue在一個(gè)線程中只會(huì)存在一個(gè)。

2、Looper.loop()會(huì)讓當(dāng)前線程進(jìn)入一個(gè)無限循環(huán),不端從MessageQueue的實(shí)例中讀取消息,然后回調(diào)msg.target.dispatchMessage(msg)方法。

3、Handler的構(gòu)造方法,會(huì)首先得到當(dāng)前線程中保存的Looper實(shí)例,進(jìn)而與Looper實(shí)例中的MessageQueue想關(guān)聯(lián)。

4、Handler的sendMessage方法,會(huì)給msg的target賦值為handler自身,然后加入MessageQueue中。

5、在構(gòu)造Handler實(shí)例時(shí),我們會(huì)重寫handleMessage方法,也就是msg.target.dispatchMessage(msg)最終調(diào)用的方法。

好了,總結(jié)完成,大家可能還會(huì)問,那么在Activity中,我們并沒有顯示的調(diào)用Looper.prepare()和Looper.loop()方法,為啥Handler可以成功創(chuàng)建呢,這是因?yàn)樵贏ctivity的啟動(dòng)代碼中,已經(jīng)在當(dāng)前UI線程調(diào)用了Looper.prepare()和Looper.loop()方法。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 香港新时代国际美容美发化妆美甲培训学校-26年培训经验,值得信赖! | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | 北京成考网-北京成人高考网 | 散热器-电子散热器-型材散热器-电源散热片-镇江新区宏图电子散热片厂家 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 风信子发稿-专注为企业提供全球新闻稿发布服务 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 顺辉瓷砖-大国品牌-中国顺辉| 超声波_清洗机_超声波清洗机专业生产厂家-深圳市好顺超声设备有限公司 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 分轨 | 上传文件,即刻分离人声和伴奏 | 长沙中央空调维修,中央空调清洗维保,空气能热水工程,价格,公司就找维小保-湖南维小保环保科技有限公司 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 楼承板-开口楼承板-闭口楼承板-无锡海逵 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 「阿尔法设计官网」工业设计_产品设计_产品外观设计 深圳工业设计公司 | 圆形振动筛_圆筛_旋振筛_三次元振动筛-河南新乡德诚生产厂家 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | 焦作网 WWW.JZRB.COM | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 洛阳网站建设_洛阳网站优化_网站建设平台_洛阳香河网络科技有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 全球化工设备网—化工设备,化工机械,制药设备,环保设备的专业网络市场。 | 开业庆典_舞龙舞狮_乔迁奠基仪式_开工仪式-神挚龙狮鼓乐文化传媒 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 |