日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android線程間通信Handler源碼詳解_Android

作者:Quincy_Ye ? 更新時間: 2022-11-28 編程語言

前言

在【Android】線程間通信 - Handler之使用篇主要講了 Handler 的創建,發送消息,處理消息 三個步驟。那么接下來,我們也按照這三個步驟,從源碼中去探析一下它們具體是如何實現的。本篇是關于創建源碼的分析。

01、 用法

先回顧一下,在主線程和非主線程是如何創建 Handler 的。

//主線程
private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
    override fun handleMessage(msg: Message) {
        when (msg.what) {
            1 -> {}
        }
    }
}
//子線程
Thread {
    Looper.prepare()
    val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {}
            }
        }
    }
    handler.sendEmptyMessage(1)
    Looper.loop()
}.start()

02、源碼

Handler 一共有 7 個構造方法。但最后都會直接或間接使用到以下兩個構造方法。所以我們看看兩個方法都做了什么事情吧。

//方法 1
//Handler.java
public Handler(@Nullable 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(); \\標識 1
    if (mLooper == null) { \\ 標識 3
        throw new RuntimeException(
            "Can't create handler inside thread " + Thread.currentThread()
                    + " that has not called Looper.prepare()");
    }
    mQueue = mLooper.mQueue; \\標識 2
    mCallback = callback;
    mAsynchronous = async;
}
//方法 2
public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) {
    mLooper = looper;
    mQueue = looper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}

方法 2 更加的簡單,那么我們就從它入手吧。在其中對 4 個變量進行賦值。分別是 mLooper, mQueue, mCallback, mAsynchronous。方法 1 主要也是對 4 個變量進行賦值。它們有什么作用,我們先不管,后面會講到。我們先來看看這方法 1 和方法 2 的區別是什么?

讓我們聚焦到標識 1 和標識 2,mLooper, mQueue 來源是通過 Looper 這個類中獲取的。那讓我們跟進去看看。

//跟進標識 1
//Looper.java
public static @Nullable Looper myLooper() {
   return sThreadLocal.get();
}

看到是通過 mThreadLocal.get() 獲得一個 Looper 實例。那么 mThreadLocal 的 Looper 又是哪里來的呢?讓我們找找 mThreadLocal.set() 方法,就知道了!

//Looper.java
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.prepare() 中添加的。

這里需要注意!

  • "Only one Looper may be created per thread",每個線程只能調用一次 prepare(),這也就意味著每個線程只有一個 Looper 。

可是看回在主線程中創建 Handler 的時候,我們并沒有調用 Looper.prepare() 方法。但是也正常運行了。那是為什么呢?那是因為在 ActivityThread 中的 main()已經調用了。

//ActivityThread.java
public static void main(String[] args) {
    //...省略無關代碼
    Looper.prepareMainLooper(); \\標識 4
   //...
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);
    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }
  //...
    Looper.loop();
}
//跟進標識 4
//Looper.java
public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

這里需要注意!

  • 我們不僅不用調用,也不能調用。否則將會觸發 IllegalStateException("The main Looper has already been prepared.") 異常。
  • 但是如果不是為主線程創建 Handler 的時候,我們就需要手動調用 Looper.prepare(), 否則該線程中的 Looper 為空,會觸發標識 3 的代碼塊。
  • 即會得到RuntimeException("Can't create handler inside thread " + Thread.currentThread()+ " that has not called Looper.prepare()")異常。

03、結語

圖源 |《第一行代碼》

最后結合《第一行代碼》中異步消息處理機制的流程圖。我們可以看出 Handler 創建過程主要是準備好了 Looper, MessageQueue 和 Handler 本身。

原文鏈接:https://juejin.cn/post/7155791766631743524

欄目分類
最近更新