1. 程式人生 > >android 判斷是否在主執行緒的方法

android 判斷是否在主執行緒的方法

核心方法如下:

 public class ThreadUtils {

    public static final String TAG = "ThreadUtils";

    public static boolean isInMainThread() {
        Looper myLooper = Looper.myLooper();
        Looper mainLooper = Looper.getMainLooper();
        Log.i(TAG, "isInMainThread myLooper=" + myLooper + ";mainLooper="
+ mainLooper); return myLooper == mainLooper; } }

咱們實際測試一下,看看日誌的列印:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println("isInMainThread()=" + ThreadUtils.isInMainThread());//在這裡呼叫一下方法
}

列印日誌:

10-25 09:57:59.933 8772-8772/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=Looper (main, tid 1) {70cc4e8};mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.933 8772-8772/com.everyoo.handlerdaemon I/System.out: isInMainThread()=true

myLooper=Looper (main, tid 1) {70cc4e8}
mainLooper=Looper (main, tid 1) {70cc4e8}

說明:很顯然,當前執行緒是在主執行緒,isInMainThread()=true。

如果在主執行緒開啟一個子執行緒:

 new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("isInChlidThread()=" + ThreadUtils.isInMainThread());

            }
        }).start();

列印日誌:

10-25 09:57:59.941 8772-9006/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=null;mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.941 8772-9006/com.everyoo.handlerdaemon I/System.out: isInChlidThread()=false

注意:myLooper=null
mainLooper=Looper (main, tid 1) {70cc4e8}
為什麼myLooper=null為空呢?這是因為Android中的執行緒預設沒有一個和它綁定了的訊息迴圈(Threads by default do not have a message loop associated with them. Of course, the method works)

如果初始化Looper物件並且啟動,看程式碼:

 new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();//初始化Looper物件
                System.out.println("isInChlidThread()=" + ThreadUtils.isInMainThread());

                Looper.loop();//啟動Looper

            }
        }).start();

列印日誌:

10-25 09:57:59.950 8772-9007/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=Looper (Thread-153, tid 153) {9984c2e};mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.950 8772-9007/com.everyoo.handlerdaemon I/System.out: isInChlidThread() isThresdMeod1=false

myLooper=Looper (Thread-153, tid 153) {9984c2e}
mainLooper=Looper (main, tid 1) {70cc4e8}

雖然myLooper不為null了,但是他們卻不屬於同一個Looper物件。並不是我們的主執行緒,所以返回false。

我們還有一種更簡單方法,直接通過日誌就能看出是主執行緒還是子執行緒。並且能分析是不是為同一個子執行緒。看如下程式碼:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "onCreate: 我是ui執行緒");

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "onCreate: 我是子執行緒");


            }
        }).start();

日誌列印:

這裡寫圖片描述

ps:看,紅色標記框裡的內容。14090 就是我們的主執行緒號,而14151 就是我們的子執行緒號。至於是不是同一個子執行緒,我們通過比對執行緒號,就能輕鬆的知道啦!

相關推薦

no