日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android異步消息處理機制實現原理詳解

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

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

Looper不斷從MessageQueue消息隊列中取出一個Message,然后傳給Handle,如此循環往復,如果隊列為空,那么它會進入休眠。

這些類的主要變量

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;每個消息只能對應一個handlerRunnable callback;回調接口

MessageQueue.java

Message mMessages;

Runnable是一個空接口類,沒有變量

上一個書上的圖:

Android異步消息處理機制實現原理詳解

Handler和Thread沒有直接關系,但對應關系可以推理得到

每個Thread只對應一個Looper;

每個Looper只對應一個MessageQueue;

每個MessageQueue對應N個Message,每個Message只對應一個Handler

==》每個Thread對應N個Handler。

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

帶著一個問題看源碼:創建handler對象的線程(ui/主線程除外)為什么,必須先調用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對象時(構造方法是Handler(),Handler(Callback callback))都間接調用Handler(Callback callback, boolean async)構造方法

主要代碼是Looper.myLooper();

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

我們自己創建線程必須通過Looper.prepare()方法為當前線程設置looper對象才可以通過Looper.myLooper()方法返回looper對象,這樣在非UI線程創建handler對象時才不會報錯。'Can’t create handler inside thread that has not called Looper.prepare()'

ps:prepare(boolean quitAllowed)(這個不用我們關心,略過。。)

這個quitAlowed參數是定義消息隊列用了,看的源代碼是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消息隊列可以被quit,false消息隊列不能被quit。

主線程/UI線程的MessageQueue不能被銷毀掉。看源碼(銷毀調用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(); } }

偏離太遠了

所以得出結論:創建handler對象的線程(ui/主線程除外),必須先調用Looper.prepare()

Handler作用1:處理消息

在Looper類中處理消息是通過msg.target.dispatchMessage(msg);target就是handler對象(Message類的內部變量Handler target)將消息轉發到處理消息的對應的handler對象上,然后這個target即handler對象會在處理消息前做一個檢查

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

Handler作用2:將Message壓入MessageQueue中

handler中提供的很多發送message的方法,除了sendMessageAtFrontOfQueue()方法(直接調用enqueueMessage(queue, msg, 0);)之外,其它的發送消息方法最終都會輾轉調用到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()方法也是調用Handler中的enqueueMessage(queue, msg, uptimeMillis)方法

和sendMessageAtFrontOfQueue()方法兩者最后都會調用enqueueMessage(queue, msg, uptimeMillis)方法

區別是需要延遲uptimeMillis時間后才將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); }

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

MessageQueue的使用是在Looper中

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

Looper類

作用:與當前線程綁定,保證一個線程只會有一個Looper實例,同時一個Looper實例也只有一個MessageQueue。

對于Looper主要是prepare()和loop()兩個方法

prepare()將普通線程轉化為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進行消息處理的關鍵代碼了

看著上面的分析很復雜,總結下

1、首先Looper.prepare()為在當前線程中保存一個Looper實例(sThreadLocal.set()),然后該實例中保存一個MessageQueue對象;因為Looper.prepare()在一個線程中只能調用一次,所以MessageQueue在一個線程中只會存在一個。

2、Looper.loop()會讓當前線程進入一個無限循環,不端從MessageQueue的實例中讀取消息,然后回調msg.target.dispatchMessage(msg)方法。

3、Handler的構造方法,會首先得到當前線程中保存的Looper實例,進而與Looper實例中的MessageQueue想關聯。

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

5、在構造Handler實例時,我們會重寫handleMessage方法,也就是msg.target.dispatchMessage(msg)最終調用的方法。

好了,總結完成,大家可能還會問,那么在Activity中,我們并沒有顯示的調用Looper.prepare()和Looper.loop()方法,為啥Handler可以成功創建呢,這是因為在Activity的啟動代碼中,已經在當前UI線程調用了Looper.prepare()和Looper.loop()方法。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美/亚洲一区| 国产精品观看| 国产在线一区不卡| 欧美激情91| 精品国产精品久久一区免费式| 免费视频久久| 国产视频一区三区| 美日韩精品视频| 亚洲一区免费| 日韩精品一区二区三区av | 精品国产日韩欧美精品国产欧美日韩一区二区三区 | 欧美日韩1区2区3区| 国产精品一国产精品| 国产精品超碰| 欧美亚洲激情| 亚洲香蕉视频| 国产精品99久久精品| 精品国产乱码久久久久久1区2匹| 色综合狠狠操| 天堂成人免费av电影一区| 国产亚洲一卡2卡3卡4卡新区| 久久精品国产亚洲一区二区三区| 欧美精品高清| 亚洲美女91| 欧美三级精品| 欧美精品国产| 激情婷婷综合| 欧美精品97| 美国三级日本三级久久99| 国产精品99久久久久久董美香| 欧美肉体xxxx裸体137大胆| 欧美一区成人| 午夜在线观看免费一区| 久久国产乱子精品免费女| yellow在线观看网址| 日韩精彩视频在线观看| 99精品在线免费在线观看| 日韩va亚洲va欧美va久久| 久久国产主播| 麻豆成人av在线| 日韩精品久久理论片| 伊人久久婷婷| 久久久精品五月天| 97欧美在线视频| 久久av电影| 国产欧美日韩精品高清二区综合区 | 一区二区三区国产在线| 99久久99久久精品国产片果冰| 精品国产中文字幕第一页| 日本va欧美va精品发布| 亚洲激情偷拍| 亚洲香蕉网站| 亚洲精品一区二区妖精| 蜜桃国内精品久久久久软件9| 四虎成人av| 国产高潮在线| 老牛影视精品| 欧美日韩视频免费观看| 日韩国产网站| 成人羞羞视频播放网站| 日韩中文在线播放| 久久99国产精品视频| 国产精品成人国产| 国产精品资源| 精品视频在线你懂得| 麻豆精品久久| 日韩深夜视频| 久久久久中文| 久久一二三区| 国产乱人伦丫前精品视频| 国产精品流白浆在线观看| 国产精品免费大片| 国模大尺度视频一区二区| 久久久男人天堂| 欧美va天堂在线| 国产亚洲一区在线| 黄色成人精品网站| 亚欧成人精品| 精品日本视频| 亚洲欧美日韩国产| 日本成人一区二区| 国产精品3区| 99久久亚洲精品蜜臀| 日韩中文字幕不卡| 国产日韩高清一区二区三区在线| 欧美激情一区| 国产一区亚洲| 日韩av不卡一区二区| 国产资源在线观看入口av| 日本大胆欧美人术艺术动态| 国产欧美日韩在线一区二区| 麻豆91在线播放| 国产精品视区| 麻豆mv在线观看| 日韩国产一二三区| 欧美日一区二区| 国内精品美女在线观看| 免播放器亚洲一区| 欧美日韩国产观看视频| 日韩不卡免费视频| 亚洲婷婷在线| 日本一区二区高清不卡| 日本中文字幕一区二区| 亚洲一级影院| 国产69精品久久| 国产一精品一av一免费爽爽| 久久久久国产精品一区三寸| 麻豆精品99| 日韩黄色免费网站| 99综合视频| 不卡av一区二区| 精品丝袜在线| 国产va免费精品观看精品视频| 91精品丝袜国产高跟在线| 久久99伊人| 国产毛片久久| 国产精品普通话对白| 日韩视频在线一区二区三区| 欧美二三四区| 久久久精品网| 激情五月综合网| 国产精品亚洲欧美一级在线| 亚洲精品国产精品粉嫩| 亚洲一区欧美二区| 丝袜美腿一区二区三区| 女人天堂亚洲aⅴ在线观看| 久久国产毛片| 91久久黄色| aa亚洲婷婷| 亚洲免费一区三区| 日韩国产精品久久久久久亚洲| 亚洲小说春色综合另类电影| 在线观看免费一区二区| 日韩精品一卡二卡三卡四卡无卡| 国产亚洲精品v| 日本免费在线视频不卡一不卡二| 视频一区二区中文字幕| 亚洲在线久久| 久久精品国产999大香线蕉| 国产一区二区久久久久| 欧美精品资源| 蜜臀av亚洲一区中文字幕| 久久精品99久久久| 久久国产精品成人免费观看的软件| 亚洲精品一区二区妖精| 免费人成网站在线观看欧美高清| 欧美在线91| 欧美黄色网页| 亚洲欧美专区| 中文字幕人成乱码在线观看| 91高清一区| 日本欧美韩国一区三区| 免费看久久久| 99国内精品| 国产精品第十页| 午夜日本精品| 欧美黄页在线免费观看| 欧美日韩国产一区精品一区| 美国欧美日韩国产在线播放| 国产精品xxxav免费视频| 99视频精品全国免费| 日本成人在线一区| 国内精品99| 国产精品18| 性色av一区二区怡红| 日韩av在线播放网址| 免费观看日韩电影| 午夜av成人| 成人在线观看免费视频| 日韩二区三区四区| 亚洲女同中文字幕| 中文在线免费视频| 麻豆极品一区二区三区| 亚洲v天堂v手机在线| 久久精品国产大片免费观看| 久久精品99国产国产精| 一级欧洲+日本+国产| 成人在线超碰| 欧美日韩va| 影音先锋久久精品| 99久久久久久中文字幕一区| 国产欧美啪啪| 日韩精品一区二区三区免费视频| 亚洲性色视频| 日韩久久精品网| 色婷婷色综合| 水蜜桃久久夜色精品一区| 精品国产亚洲日本| 国产一区二区三区黄网站| 精品久久免费| 日本激情一区| 精精国产xxxx视频在线播放| 高潮一区二区| 午夜欧美巨大性欧美巨大| 四虎成人av| 亚洲精品电影| 在线亚洲激情| 亚洲综合中文| 欧美a一区二区| 高潮一区二区|