成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入探討Android異步精髓Handler

移動開發 Android
Google采用Handler把主線程和子線程精巧地聯系起來——子線程中進行耗時的業務邏輯,然后利用Handler通知主線程刷新UI。除此以外,還有別的方式可以實現類似的操作么?答案是肯定的,我們也可以利用AsyncTask或者IntentService進行異步的操作。Handler是Android異步操作的核心和精髓,它在眾多領域發揮著極其重要甚至是不可替代的作用。

前言

眾所周知,Android的UI是在其主線程中進行刷新的,所以Google建議開發人員切勿在主線程中進行耗時的操作否則很容易導致應用程序無響應(ANR)。鑒于此幾乎接近硬性的要求,我們常把耗時的操作(比如網絡請求)置于子線程中進行;但是子線程不能直接訪問UI。

至此,這個矛盾就凸顯出來了:

  • 主線程可以刷新UI,但不能執行耗時操作
  • 子線程可以執行耗時操作 ,但是不能直接刷新UI

嗯哼,那有沒有一個東西可以調和并化解這個矛盾呢?當然是有的,Google采用Handler把主線程和子線程精巧地聯系起來——子線程中進行耗時的業務邏輯,然后利用Handler通知主線程刷新UI。除此以外,還有別的方式可以實現類似的操作么?答案是肯定的,我們也可以利用AsyncTask或者IntentService進行異步的操作。這兩者又是怎么做到的呢?其實,在AsyncTask和IntentService的內部亦使用了Handler實現其主要功能。拋開這兩者不談,當我們打開Android源碼的時候也隨處可見Handler的身影。所以,Handler是Android異步操作的核心和精髓,它在眾多領域發揮著極其重要甚至是不可替代的作用。

在此,對Handler的工作原理和實現機制進行系統的梳理。

ThreadLocal簡介及其使用

對于線程Thread大家都挺熟悉的了,但是對于ThreadLocal可能就要陌生許多了。雖然我們對于它不太了解,但是它早在JDK1.2版本中就已問世并且被廣泛的使用,比如hibernate,EventBus,Handler都運用了ThreadLocal進行線程相關的操作。如果單純地從ThreadLocal這個名字來看,它帶著濃濃的“本地線程”的味道; 然而,喝一口之后才發現根本就不是這個味兒。其實,ThreadLocal并不是用來操作什么本地線程而是用于實現不同線程的數據副本。當使用ThreadLocal維護變量時,它會為每個使用該變量的線程提供獨立的變量副本;每一個線程都可以獨立地改變自己的副本并且不會影響其它線程所持有的對應的副本。所以,ThreadLocal的實際作用并不與它的名字所暗含的意義相吻合,或許改稱為ThreadLocalVariable(線程本地變量)會更合適一些。

接下來,我們通過一個實例來瞅瞅ThreadLocal的使用方式

  1. /** 
  2.      * 原創作者: 
  3.      * 谷哥的小弟 
  4.      * 
  5.      * 博客地址: 
  6.      * http://blog.csdn.net/lfdfhl 
  7.      */ 
  8.     private void testThreadLocal(){ 
  9.         mThreadLocal.set("東京熱"); 
  10.         new HotThread1().start(); 
  11.         new HotThread2().start(); 
  12.         hot3=mThreadLocal.get(); 
  13.         try{ 
  14.             Thread.sleep(1000*4); 
  15.             Log.i(TAG,"HotThread1獲取到的變量值: "+hot1); 
  16.             Log.i(TAG,"HotThread2獲取到的變量值: "+hot2); 
  17.             Log.i(TAG,"MainThread獲取到的變量值: "+hot3); 
  18.         }catch (Exception e){ 
  19.  
  20.         } 
  21.     } 
  22.  
  23.     private class HotThread1  extends Thread{ 
  24.         @Override 
  25.         public void run() { 
  26.             super.run(); 
  27.             mThreadLocal.set("北京熱"); 
  28.             hot1=mThreadLocal.get(); 
  29.         } 
  30.     } 
  31.  
  32.     private class HotThread2  extends Thread{ 
  33.         @Override 
  34.         public void run() { 
  35.             super.run(); 
  36.             mThreadLocal.set("南京熱"); 
  37.             hot2=mThreadLocal.get(); 
  38.         } 
  39.     }  

查看輸出結果:

  1. HotThread1獲取到的變量值: 北京熱 
  2. HotThread2獲取到的變量值: 南京熱 
  3. MainThread獲取到的變量值: 東京熱  

在這段代碼中使用ThreadLocal保存String類型的數據,并且在主線程和兩個子線程中為ThreadLocal設置了不同的值,然后再將這些值分別取出。結合輸出日志可以發現:在不同的線程中訪問了同一個ThreadLocal對象,但是通過mThreadLocal.get()得到的值卻是不一樣的;也就是說:它們之間沒有發生相互的影響而是保持了彼此的獨立。明白了ThreadLocal的這個特性之后,我們再去理解Looper的工作機制就會容易得多了。

Looper、線程、消息隊列的關系

Google官方建議開發人員使用Handler實現異步刷新UI,我們在平常的工作中也很好地采納了這個提議:首先在主線程中建立Handler,然后在子線程中利用handler.sendMessage(message)發送消息至主線程,最終消息在handleMessage(Message msg) {}得到相應的處理。這個套路,大家都再熟悉不過了;現在換個角度,我們試試在子線程中建立Handler

  1. private class LooperThread  extends Thread{ 
  2.         @Override 
  3.         public void run() { 
  4.             super.run(); 
  5.             Handler handler=new Handler(); 
  6.             //doing something 
  7.         } 
  8.     }  

此處的代碼很簡單:LooperThread繼承自Thread,并且在其run( )方法中新建一個Handler。

嗯哼,再運行一下,喔哦,報錯了:

  1. Can’t create handler inside thread that has not called Looper.prepare(). 

咦,有點出師不利呢,剛開始試就出錯了…….沒事,生活不就是無盡的挫折和希望嘛,這點小事嘛也不算。既然是在調用Handler的構造方法時報的錯那就從該構造方法的源碼入手,一探究竟:

  1. public Handler() { 
  2.     this(nullfalse); 
  3.  
  4. public Handler(Callback callback) { 
  5.     this(callback, false); 
  6.  
  7. public Handler(Callback callback, boolean async) { 
  8.     if (FIND_POTENTIAL_LEAKS) { 
  9.         final Class<? extends Handler> klass = getClass(); 
  10.         if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && 
  11.                 (klass.getModifiers() & Modifier.STATIC) == 0) { 
  12.             Log.w(TAG, "The following Handler class should be static or leaks might occur"); 
  13.         } 
  14.     } 
  15.  
  16.     mLooper = Looper.myLooper(); 
  17.     if (mLooper == null) { 
  18.         throw new RuntimeException 
  19.         ("Can't create handler inside thread that has not called Looper.prepare()"); 
  20.     } 
  21.     mQueue = mLooper.mQueue; 
  22.     mCallback = callback; 
  23.     mAsynchronous = async; 
  24.  

請注意第20行代碼:

如果mLooper == null那么系統就會拋出剛才的錯誤:Can’t create handler inside thread that has not called Looper.prepare()。這句話的意思是:如果在線程內創建handler必須調用Looper.prepare()。既然這個提示已經提示了我們該怎么做,那就加上這一行代碼:

  1. private class LooperThread  extends Thread{ 
  2.         @Override 
  3.         public void run() { 
  4.             super.run(); 
  5.             Looper.prepare(); 
  6.             Handler handler=new Handler(); 
  7.             System.out.println("add code : Looper.prepare()"); 
  8.             //doing something 
  9.         } 
  10.     }  

嘿嘿,果然不再報錯了,運行一下:

 

既然Looper.prepare()解決了這個問題,那我們就去瞅瞅在該方法中做了哪些操作:

  1. /**Initialize the current thread as a looper. 
  2.  * This gives you a chance to create handlers that then reference 
  3.  * this looper, before actually starting the loop. Be sure to call 
  4.  * loop() after calling this method, and end it by calling quit(). 
  5.  */ 
  6. public static void prepare() { 
  7.     prepare(true); 
  8.  
  9. private static void prepare(boolean quitAllowed) { 
  10.     if (sThreadLocal.get() != null) { 
  11.         throw new RuntimeException("Only one Looper may be created per thread"); 
  12.     } 
  13.     sThreadLocal.set(new Looper(quitAllowed)); 
  14.  

從這段源碼及其注釋文檔我們可以看出:

在prepare()中利用一個Looper來初始化當前線程或者說初始化一個帶有Looper的線程。

請注意第14行代碼,它是這段源碼的核心,現對其詳細分析:

  1. sThreadLocal.set(new Looper(quitAllowed)); 

在該行代碼中一共執行了兩個操作

(1) 構造Looper

  1. private Looper(boolean quitAllowed) { 
  2.     mQueue = new MessageQueue(quitAllowed); 
  3.     mThread = Thread.currentThread(); 
  4.  

在Looper的構造方法中初始化了一個消息隊列MessageQueue和一個線程Thread。從這可看出:一個Looper對應著一個消息隊列以及當前線程。

當收到消息Message后系統會將其存入消息隊列中等候處理。至于Looper,它在Android的消息機制中擔負著消息輪詢的職責,它會不間斷地查看MessageQueue中是否有新的未處理的消息;若有則立刻處理,若無則進入阻塞。

(2) 將此Looper保存到sThreadLocal中。

此處的sThreadLocal是定義在Looper類中的一個ThreadLocal類型變量

  1. static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); 

Looper是framework中的一個類,sThreadLocal是它的一個static final變量。當在某一個Thread中執行Looper.prepare()時系統就會將與該Thread所對應的Looper保存到sThreadLocal中。不同的線程對著不同的Looper,但它們均由系統保存在sThreadLocal中并且互不影響,相互獨立;并且可以通過sThreadLocal.get()獲取不同線程所對應的Looper.

在調用prepare()方法后需要調用loop()方法開始消息的輪詢,并且在需要的時候調用quit()方法停止消息的輪詢

假若再次執行Looper.prepare()系統發現sThreadLocal.get()的值不再為null于是拋出異常:

Only one Looper may be created per thread,一個線程只能創建一個Looper!

小結:

  1. 一個線程對應一個Looper
  2. 一個Looper對應一個消息隊列
  3. 一個線程對應一個消息隊列
  4. 線程,Looper,消息隊列三者一一對應

所以,在一個子線程中使用Handler的方式應該是這樣的:

  1. class LooperThread extends Thread {  
  2.     public Handler mHandler; 
  3.     public void run() {  
  4.         Looper.prepare();  
  5.         mHandler = new Handler() {  
  6.             public void handleMessage(Message msg) {  
  7.  
  8.             }  
  9.         }; 
  10.         Looper.loop();  
  11.       }  
  12.   }  

看到這個范例,有的人可能心里就犯嘀咕了:為什么我們平常在MainActivity中使用Handler時并沒有調用Looper.prepare()也沒有報錯呢?

這是因為UI線程是主線程,系統會自動調用Looper.prepareMainLooper()方法創建主線程的Looper和消息隊列MessageQueue

Message的發送和處理過程

在討論完Looper、線程、消息隊列這三者的關系之后我們再來瞅瞅Android消息機制中對于Message的發送和處理。

平常最常用的方式:

handler.sendMessage(message)——>發送消息

handleMessage(Message msg){}——>處理消息

先來分析消息的入隊。

Handler可以通過post()、postAtTime()、postDelayed()、postAtFrontOfQueue()等方法發送消息,除了postAtFrontOfQueue()之外這幾個方法均會執行到sendMessageAtTime(Message msg, long uptimeMillis)方法,源碼如下:

  1. public boolean sendMessageAtTime(Message msg, long uptimeMillis) { 
  2.     MessageQueue queue = mQueue; 
  3.     if (queue == null) { 
  4.         RuntimeException e = new RuntimeException( 
  5.                 this + " sendMessageAtTime() called with no mQueue"); 
  6.         Log.w("Looper", e.getMessage(), e); 
  7.         return false
  8.     } 
  9.     return enqueueMessage(queue, msg, uptimeMillis); 
  10.  
  11. public final boolean sendMessageAtFrontOfQueue(Message msg) { 
  12.     MessageQueue queue = mQueue; 
  13.     if (queue == null) { 
  14.         RuntimeException e = new RuntimeException( 
  15.                 this + " sendMessageAtTime() called with no mQueue"); 
  16.         Log.w("Looper", e.getMessage(), e); 
  17.         return false
  18.     } 
  19.     return enqueueMessage(queue, msg, 0); 
  20.  
  21. private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { 
  22.     msg.target = this; 
  23.     if (mAsynchronous) { 
  24.         msg.setAsynchronous(true); 
  25.     } 
  26.     return queue.enqueueMessage(msg, uptimeMillis); 
  27.  

在這里可以看到sendMessageAtTime()內部又調用了enqueueMessage(),在該方法內的重要操作:

  • ***步:

給msg設置了target,請參見代碼第25行此處的this就是當前Handler對象本身。在這就指明了該msg的來源——它是由哪個Handler發出的,與此同時也指明了該msg的歸宿——它該由哪個Handler處理。不難發現,哪個Handler發出了消息就由哪個Handler負責處理。

  • 第二步:

將消息放入消息隊列中,請參見代碼第29行在enqueueMessage(msg,uptimeMillis)中將消息Message存放進消息隊列中,距離觸發時間最短的message排在隊列最前面,同理距離觸發時間最長的message排在隊列的最尾端。若調用sendMessageAtFrontOfQueue()方法發送消息它會直接調用該enqueueMessage(msg,uptimeMillis)讓消息入隊只不過時間為延遲時間為0,也就是說該消息會被插入到消息隊列頭部優先得到執行。直覺告訴我們此處的消息隊列mQueue就是該線程所對應的消息隊列。可是光有直覺是不夠的甚至是不可靠的。我們再回過頭瞅瞅Handler的構造方法,從源碼中找到確切的依據 

  1. public Handler(Callback callback, boolean async) { 
  2.     if (FIND_POTENTIAL_LEAKS) { 
  3.         final Class<? extends Handler> klass = getClass(); 
  4.         if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && 
  5.                 (klass.getModifiers() & Modifier.STATIC) == 0) { 
  6.             Log.w(TAG, "The following Handler class should be static or leaks might occur"); 
  7.         } 
  8.     } 
  9.  
  10.     mLooper = Looper.myLooper(); 
  11.     if (mLooper == null) { 
  12.         throw new RuntimeException 
  13.         ("Can't create handler inside thread that has not called Looper.prepare()"); 
  14.     } 
  15.     mQueue = mLooper.mQueue; 
  16.     mCallback = callback; 
  17.     mAsynchronous = async; 
  18.  

(1) 獲取Looper,請參見代碼第10行

(2) 利用Looper的消息隊列為mQueue賦值,請參見代碼第15行

(3) 為mCallback賦值,,請參見代碼第16行

(4) 為mAsynchronous賦值,,請參見代碼第17行

嗯哼,看到了吧,這個mQueue就是從Looper中取出來的。在之前我們也詳細地分析了Looper、線程、消息隊列這三者的一一對應關系,所以此處的mQueue正是線程所對應的消息隊列。

看完了消息的入隊,再來分析消息的出隊。

請看Looper中的loop()方法源碼: 

  1. public static void loop() { 
  2.     final Looper me = myLooper(); 
  3.     if (me == null) { 
  4.         throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); 
  5.     } 
  6.     final MessageQueue queue = me.mQueue; 
  7.  
  8.     // Make sure the identity of this thread is that of the local process, 
  9.     // and keep track of what that identity token actually is
  10.     Binder.clearCallingIdentity(); 
  11.     final long ident = Binder.clearCallingIdentity(); 
  12.  
  13.     for (;;) { 
  14.         Message msg = queue.next(); // might block 
  15.         if (msg == null) { 
  16.             // No message indicates that the message queue is quitting. 
  17.             return
  18.         } 
  19.  
  20.         // This must be in a local variable, in case a UI event sets the logger 
  21.         final Printer logging = me.mLogging; 
  22.         if (logging != null) { 
  23.             logging.println(">>>>> Dispatching to " + msg.target + " " + 
  24.                     msg.callback + ": " + msg.what); 
  25.         } 
  26.  
  27.         final long traceTag = me.mTraceTag; 
  28.         if (traceTag != 0) { 
  29.             Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); 
  30.         } 
  31.         try { 
  32.             msg.target.dispatchMessage(msg); 
  33.         } finally { 
  34.             if (traceTag != 0) { 
  35.                 Trace.traceEnd(traceTag); 
  36.             } 
  37.         } 
  38.  
  39.         if (logging != null) { 
  40.             logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); 
  41.         } 
  42.  
  43.         // Make sure that during the course of dispatching the 
  44.         // identity of the thread wasn't corrupted. 
  45.         final long newIdent = Binder.clearCallingIdentity(); 
  46.         if (ident != newIdent) { 
  47.             Log.wtf(TAG, "Thread identity changed from 0x" 
  48.                     + Long.toHexString(ident) + " to 0x" 
  49.                     + Long.toHexString(newIdent) + " while dispatching to " 
  50.                     + msg.target.getClass().getName() + " " 
  51.                     + msg.callback + " what=" + msg.what); 
  52.         } 
  53.  
  54.         msg.recycleUnchecked(); 
  55.     } 
  56.  

我們發現關于消息的處理是在一個死循環中就行的,請參見代碼第13-55行。也就是說在該段代碼中Looper一直在輪詢消息隊列MessageQueue。假若消息隊列中沒有未處理的消息(即queue.next()==null)則其進入阻塞block狀態,假若消息隊列中有待處理消息(即queue.next()!=null)則利用msg.target.dispatchMessage(msg)將該消息派發至對應的Handler。

到了這,可能有的人會有一個疑問:系統怎么知道把消息發送給哪個Handler呢?

嘿嘿,還記不記得enqueueMessage()中系統給msg設置了target從而確定了其目標Handler么?嗯哼,所以只要通過msg.target.dispatchMessage(msg)就可以將消息派發至對應的Handler了。那在dispatchMessage()中又會對消息做哪些操作呢?我們繼續跟進源碼

  1. public void dispatchMessage(Message msg) { 
  2.     if (msg.callback != null) { 
  3.         handleCallback(msg); 
  4.     } else { 
  5.         if (mCallback != null) { 
  6.             if (mCallback.handleMessage(msg)) { 
  7.                 return
  8.             } 
  9.         } 
  10.         handleMessage(msg); 
  11.     } 
  12.  

哇哈,看到這,心情就舒暢多了,基本上回到了我們熟悉的地方;在此處對Message消息進行了處理,我們來瞅瞅主要的步驟

  • ***步:

處理Message的回調callback,請參見代碼第3行

比如調用handler.post(Runnable runnable)時,該runnable就會被系統封裝為Message的callback。

關于這點在源碼中也有非常直觀的體現:

  1. private static Message getPostMessage(Runnable r) { 
  2.    Message m = Message.obtain(); 
  3.    m.callback = r; 
  4.    return m; 
  5.  
  • 第二步:

處理Handler的回調callback,請參見代碼第6行

比如執行Handler handler=Handler(Callback callback)時就會將callback賦值給mCallback,關于這點已經在介紹Handler構造方法時分析過了,不再贅述。

第三步:

調用handleMessage()處理消息Message,請參見代碼第10行

handleMessage()的源碼如下:

  1. public void handleMessage(Message msg) { 
  2.  
  3.  

嗯哼,它是一個空的方法。所以Handler的子類需要覆寫該方法,并在其中處理接收到的消息。

梳理Handler工作機制

至此,關于Handler的異步機制及其實現原理已經分析完了。在此,對其作一個全面的梳理和總結。

Android異步消息機制中主要涉及到:Thread、Handler、MessageQueue、Looper,在整個機制中它們扮演著不同的角色也承擔著各自的不同責任。

  • Thread負責業務邏輯的實施。

線程中的操作是由各自的業務邏輯所決定的,視具體情況進行。

  • Handler負責發送消息和處理消息。

通常的做法是在主線程中建立Handler并利用它在子線程中向主線程發送消息,在主線程接收到消息后會對其進行處理

  • MessageQueue負責保存消息。

Handler發出的消息均會被保存到消息隊列MessageQueue中,系統會根據Message距離觸發時間的長短決定該消息在隊列中位置。在隊列中的消息會依次出隊得到相應的處理。

  • Looper負責輪詢消息隊列。

Looper使用其loop()方法一直輪詢消息隊列,并在消息出隊時將其派發至對應的Handler.

為了更好地理解這幾者的相互關系及其作用,請參見如下示圖 

 

 

 

使用Handler的錯誤姿勢及其潛在風險

關于Handler的具體用法,尤其是那些常規的使用方式在此就不再一一列舉了。

責任編輯:龐桂玉 來源: Android開發中文站
相關推薦

2012-02-28 14:43:43

2009-12-23 16:13:00

WPF Attache

2009-11-20 17:17:08

Oracle函數索引

2021-05-17 05:36:02

CSS 文字動畫技巧

2010-11-22 14:18:32

MySQL鎖機制

2010-07-21 09:38:15

PHP緩存技術

2010-01-26 13:47:57

Android電話功能

2009-11-12 13:56:54

2009-12-07 16:07:03

PHP類的繼承

2009-12-14 14:40:10

Ruby全局域變量

2024-01-26 06:42:05

Redis數據結構

2013-07-11 09:45:48

扁平化扁平化設計

2009-10-16 09:17:39

屏蔽布線系統

2009-12-07 13:55:58

PHP array_m

2009-12-11 11:08:31

靜態路由策略

2010-03-05 13:44:00

Python序列

2009-08-27 11:27:58

foreach語句C# foreach語

2023-01-12 17:18:06

數據庫多云

2011-02-25 09:23:00

Java類加載器

2010-03-31 14:58:03

云計算
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品国产乱码久久久久久闺蜜 | 99热精品6| 毛片在线看看 | 午夜影院在线观看免费 | 日韩av在线一区二区 | 精品伊人久久 | 天天干天天干 | 成人精品久久 | 男人天堂网址 | 日韩一区二区在线视频 | 国产午夜精品久久久 | 热久久久 | 国产精品色综合 | 中文字幕日韩欧美一区二区三区 | 九九热免费看 | 成人av电影免费在线观看 | 999国产精品视频免费 | a在线视频| 91伊人 | 久久成人精品 | 久久99精品久久久久蜜桃tv | 国产精品久久久久9999鸭 | 午夜丁香视频在线观看 | 亚洲一区二区在线视频 | 国产激情精品一区二区三区 | 成人免费看黄 | 亚洲精品视频在线播放 | 午夜资源| 日韩一级黄色毛片 | 国产激情视频在线 | 欧美一区二区三区在线观看视频 | 日韩欧美在线视频一区 | 国产精品高潮呻吟久久 | 国产三区av| 国产精品久久久久免费 | 中文无吗| 一区二区三区不卡视频 | 国产区视频在线观看 | 97人人爱 | 亚洲欧美精 | 国产一级在线视频 |