最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

從源碼角度分析Android的消息機制

 更新時間:2021年03月29日 09:19:56   作者:阿沐洛  
這篇文章主要介紹了從源碼角度分析Android的消息機制,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下

前言

說到Android的消息機制,那么主要的就是指的Handler的運行機制。其中包括MessageQueue以及Looper的工作過程。

在開始正文之前,先拋出兩個問題:

  1. 為什么更新UI的操作要在主線程中進行?
  2. Android中為什么主線程不會因為Looper.loop()里的死循環(huán)卡死?

UI線程的判斷是在ViewRootImpl中的checkThread方法中完成的。

對于第一個問題,這里給一個簡單的回答:

如果可以在子線程中修改UI,多線程的并發(fā)訪問可能會導致UI控件的不可預期性,采用加鎖的方式,就會降低UI的訪問效率以及會阻塞其他線程的執(zhí)行,所以最簡單有效的方法就是采用單線程模型來處理UI操作。

Handler的運行離不來底層的MessageQueue和Looper的支撐。MessageQueue翻譯過來是一個消息隊列,里面存儲了Handler需要的Message,MessageQueue并不是一個隊列,其實上是用單鏈表的數(shù)據(jù)結(jié)構來存儲Message。

那么Handler如何拿到Message呢?這時候就需要Looper了,Looper通過Looper.loop()來開啟一個死循環(huán),不斷從MessageQueue中取消息然后傳遞給Handler。

這里還有另一個知識點就是Looper的獲取,這里就要提高一個存儲類:ThreadLocal

ThreadLocal的工作原理

ThreadLocal是線程內(nèi)部的一個數(shù)據(jù)存儲類,可以存儲某個線程中的數(shù)據(jù),對于其他線程無法獲取該線程的數(shù)據(jù)。我們通過原理來看一下,這個觀點是否正確。

 public void set(T value) {
  Thread t = Thread.currentThread();
  ThreadLocalMap map = getMap(t);
  if (map != null)
   map.set(this, value);
  else
   createMap(t, value);
 }
 
  public T get() {
  Thread t = Thread.currentThread();
  ThreadLocalMap map = getMap(t);
  if (map != null) {
   ThreadLocalMap.Entry e = map.getEntry(this);
   if (e != null) {
    @SuppressWarnings("unchecked")
    T result = (T)e.value;
    return result;
   }
  }
  return setInitialValue();
 }

可以看出它的set和get方法就是在當前線程中所做的操作,ThreadLocalMap內(nèi)部是一個數(shù)組table。 這樣就保證了在不同線程中的數(shù)據(jù)互不干擾。

ThreadLocal除了使用在Handler中獲取Looper,還用于一些復雜的場景,比如:監(jiān)聽器的傳遞。

我們簡單了解了ThreadLocal,那么我們從New Handler()來一步步梳理下消息機制。

Looper的工作原理

// Handler.java

 public Handler() {
  this(null, false);
 }
 // callback 消息回調(diào);async 是否同步
 public Handler(Callback callback, boolean async) {
  ...
  // 1. 首先獲取looper
  mLooper = Looper.myLooper();
  if (mLooper == null) {
   throw new RuntimeException(
    "Can't create handler inside thread " + Thread.currentThread()
      + " that has not called Looper.prepare()");
  }
  // 2. 獲取MessggeQueue
  mQueue = mLooper.mQueue;
  mCallback = callback;
  mAsynchronous = async;
 }

我們平常用的是無參數(shù)的方法,它傳入的是空的回調(diào)以及false。

 public static @Nullable Looper myLooper() {
  return sThreadLocal.get();
 }

這里就出現(xiàn)了我們之前說的ThreadLoacal類,那么looper值是什么時候設置進去的呢?

它的設置方法其實是在prepare方法以及prepareMainLooper方法中,我們來分別來看下:

 public static void prepare() {
  prepare(true);
 }

 private static void prepare(boolean quitAllowed) {
  // 在創(chuàng)建looper之前,判斷l(xiāng)ooper是否與threadloacal綁定過,這也是prepare只能設置一遍的原因。
  if (sThreadLocal.get() != null) {
   throw new RuntimeException("Only one Looper may be created per thread");
  }
  sThreadLocal.set(new Looper(quitAllowed));
 }

 public static void prepareMainLooper() {
  // 這里其實還是調(diào)用的prepare方法
  prepare(false);
  synchronized (Looper.class) {
   if (sMainLooper != null) {
    throw new IllegalStateException("The main Looper has already been prepared.");
   }
   sMainLooper = myLooper();
  }
 }

通過上面可以prepare方法只能設置一遍,那么我們在主線程中為什么能直接使用呢? app程序的入口是在ActivityThread中的main方法中:

public static void main(String[] args) {
  ...

  //1. 初始化Looper對象
  Looper.prepareMainLooper();
  
  // 2. 開啟無限循環(huán)
  Looper.loop();
  throw new RuntimeException("Main thread loop unexpectedly exited");
 }

看到了吧,初始化在這里,那么我們再來看下looper的初始化方法:

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

Looper的初始化做了兩件事:創(chuàng)建消息隊列MessageQueue以及獲取當前的線程。 到這里,我們可以得到一個結(jié)論:

  • prepare方法在一個線程中只能調(diào)用一次。
  • Looper的初始化在一個線程中只能調(diào)用一次。
  • 最后可以得知:一個線程對應一個Looper,一個Looper對應一個MessageQueue。

Looper可以理解為一個工廠線,不斷從MessageQueue中取Message,工廠線開啟的方式就是Looper.loop()

 public static void loop() {
  final Looper me = myLooper();
  // 1. 判斷l(xiāng)ooper是否存在
  if (me == null) {
   throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
  }
  final MessageQueue queue = me.mQueue;
  ...
  
  //2. 開啟一個死循環(huán)
  for (;;) {
   Message msg = queue.next(); // might block
   if (msg == null) {
    // No message indicates that the message queue is quitting.
    return;
   }
   ...
   try {
    msg.target.dispatchMessage(msg);
    dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
   } finally {
    if (traceTag != 0) {
     Trace.traceEnd(traceTag);
    }
   }
  ...
  }
 }

looper方法通過開啟一個死循環(huán),不斷從MessageQueue中取Message消息,當message為空時,退出該循環(huán),否則調(diào)用msg.target.dispatchMessage(msg)方法,target就是msg綁定的Handler對象。

Handler的工作原理

好了到這里又回到了Handler類中。

 public void dispatchMessage(Message msg) {
  if (msg.callback != null) {
   handleCallback(msg);
  } else {
   if (mCallback != null) {
    if (mCallback.handleMessage(msg)) {
     return;
    }
   }
   handleMessage(msg);
  }
 }

這個handleMessage就是我們需要實現(xiàn)的方法。 那么Handler是如何設置到Message中的呢?我們來看下我們熟知的sendMessage方法:

 public final boolean sendMessage(Message msg)
 {
  return sendMessageDelayed(msg, 0);
 }
 
 public final boolean sendMessageDelayed(Message msg, long delayMillis)
 {
  if (delayMillis < 0) {
   delayMillis = 0;
  }
  return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
 }
 
  public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
  MessageQueue queue = mQueue;
  ...
  return enqueueMessage(queue, msg, uptimeMillis);
 }
 
 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
  // 關鍵代碼來了!
  msg.target = this;
  if (mAsynchronous) {
   msg.setAsynchronous(true);
  }
  return queue.enqueueMessage(msg, uptimeMillis);
 }

可以看到,通過一系列的方法,在enqueueMessage中將handler賦值到msg的target中。最后調(diào)用的是MessageQueue的enqueueMessage方法中:

 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 {
    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.next
    prev.next = msg;
   }

   // We can assume mPtr != 0 because mQuitting is false.
   if (needWake) {
    nativeWake(mPtr);
   }
  }
  return true;
 }

enqueueMessage方法主要做了兩件事:

首先判斷handler是否存在以及是否在使用中。然后根據(jù)時間順序插入MessageQueue中。

到這里基本的流程已經(jīng)梳理完了,回到起初我們的問題:Looper.loop()是一個死循環(huán),為什么不會堵塞主線程呢?

我們來看下MessageQueue的next方法:

 Message next() {
  final long ptr = mPtr;
  if (ptr == 0) {
   return null;
  }

  int pendingIdleHandlerCount = -1; // -1 only during first iteration
  int nextPollTimeoutMillis = 0;
  for (;;) {

   nativePollOnce(ptr, nextPollTimeoutMillis);
   ...
  }
 }
 

nativePollOnce方法是一個 native 方法,當調(diào)用此 native 方法時,主線程會釋放 CPU 資源進入休眠狀態(tài),直到下條消息到達或者有事務發(fā)生,通過往 pipe 管道寫端寫入數(shù)據(jù)來喚醒主線程工作,這里采用的 epoll 機制。關于 nativePollOnce 的詳細分析可以參考:nativePollOnce函數(shù)分析

總結(jié)

  1. app程序啟動從ActivityThread中的main方法中開始,通過Looper.prepare()進行Looper以及MessageQueue的創(chuàng)建以及ThreadLocal與線程之間的綁定。
  2. 我們在創(chuàng)建Handler時,通過ThreadLocal來獲取該線程中的Looper以及在Looper上綁定的MessageQueue。
  3. 通過Handler.sendMessage()方法來將msg與Handler之間進行綁定,然后將msg通過時間順序插入MessageQueue中。
  4. 主線程創(chuàng)建后,Looper.loop()來啟動一個(不占用資源)死循環(huán),從Looper已經(jīng)存在的MessageQueue中不斷取出Message,然后調(diào)用不為空的Message綁定的Handler的dispatchMessage(msg)方法,最后會調(diào)用我們復寫的handlerMessage方法中。

參考資料

Androi開發(fā)藝術探索

以上就是從源碼角度分析Android的消息機制的詳細內(nèi)容,更多關于Android 消息機制的資料請關注腳本之家其它相關文章!

相關文章

最新評論

甘泉县| 苍山县| 沂南县| 蓬莱市| 谷城县| 合作市| 徐水县| 临泉县| 吴堡县| 奉节县| 浦江县| 日喀则市| 山阳县| 正阳县| 宁强县| 平邑县| 玉山县| 紫阳县| 始兴县| 灌南县| 通海县| 哈巴河县| 彰武县| 策勒县| 浙江省| 临江市| 屯昌县| 太仆寺旗| 章丘市| 宣汉县| 长春市| 赤水市| 永清县| 十堰市| 大冶市| 临江市| 徐水县| 抚顺县| 十堰市| 墨江| 区。|