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();

日志打印:

android 判断是否在主线程的方法_第1张图片

ps:看,红色标记框里的内容。14090 就是我们的主线程号,而14151 就是我们的子线程号。至于是不是同一个子线程,我们通过比对线程号,就能轻松的知道啦!

你可能感兴趣的:(【android】)