Handler异步消息传递机制(三)在主线程、子线程中创建Handler,源码(Android 9.0)解析

版权声明:本文为博主原创文章,不得随意转载,转载请注明出处!!! https://blog.csdn.net/YuDBL/article/details/86684853

声明:本教程不收取任何费用,欢迎转载,尊重作者劳动成果,不得用于商业用途,侵权必究!!!

文章目录

一、前言

二、为什么子线程不调用Looper.prepare(),创建Handler后会报错呢?

三、主线程中的Handler之前也没有调用Looper.prepare()方法,为什么就没有崩溃呢?

四、总结注意点:


一、前言

上篇文章 Handler异步消息传递机制(二)Handler在主线程new还是子线程new?我们在子线程创建了Handler,程序崩溃提示报错信息为:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(),

根据错误信息然后我们试试在线程中先调用一下Looper.prepare(),再创建Handler对象。bug解决,程序不再崩溃!

                Looper.prepare();
                handler = new Handler() {
                。。。。。。

 

二、为什么子线程不调用Looper.prepare(),创建Handler后会报错呢?

我们来看下Handler的源码,搞清楚为什么不调用Looper.prepare()就不行呢。Handler的无参构造函数如下所示:

   public Handler() {
        this(null, false);
    }

然后继续跟踪我们可以看到如下代码:

    public Handler(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();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread " + Thread.currentThread()
                        + " that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

可以看到,在第11行调用了Looper.myLooper()方法获取了一个Looper对象,

如果Looper对象为空,则会抛出一个运行时异常,提示的错误信息正是:"Can't create handler inside thread " + Thread.currentThread() + " that has not called Looper.prepare()",

那么什么时候Looper对象mLooper才会为空呢?这就要看看Looper.myLooper()中的代码了,我们继续跟踪源码:

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

我们发现就是从sThreadLocal对象中取出Looper,然后进行return返回。

如果从sThreadLocal中get到有Looper存在就返回Looper,如果没有Looper存在自然就返回null了。

而返回null就会报如上错误,然而我们通过错误提示信息在创建Handler之前执行Looper.prepare()就不报错了。

我们自然可以推断出当执行Looper.prepare(),Looper对象mLooper不为null,即解决了报"Can't create handler inside thread " + Thread.currentThread() + " that has not called Looper.prepare()",的异常信息。

所以Looper.prepare()方法我们来看一下它的源代码:

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

然后继续跟踪我们可以看到如下代码:

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

通过代码我们知道,首先判断sThreadLocal通过get方法,是否能获取到Looper对象。

如果有的话,就会提示错误信息:“java.lang.RuntimeException: Only one Looper may be created per thread”,直译过来就是“每个线程只能创建一个 Looper对象”,你可以二次调用Looper.prepare(); 想·验证下。

如果还没有则创建一个新的Looper设置进去。这样也就完全解释了为什么我们要先调用Looper.prepare()方法,才能创建Handler对象。同时也可以看出每个线程中最多只会有一个Looper对象。

三、主线程中的Handler之前也没有调用Looper.prepare()方法,为什么就没有崩溃呢?

Android的主线程就是ActivityThread,主线程的入口方法为main,我们来看一下ActivityThread类中main方法的源代码:

public static void main(String[] args) {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");

        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);

        Environment.initForCurrentUser();

        // Set the reporter for event logging in libcore
        EventLogger.setReporter(new EventLoggingReporter());

        // Make sure TrustedCertificateStore looks in the right place for CA certificates
        final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
        TrustedCertificateStore.setDefaultUserDirectory(configDir);

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();

        // Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.
        // It will be in the format "seq=114"
        long startSeq = 0;
        if (args != null) {
            for (int i = args.length - 1; i >= 0; --i) {
                if (args[i] != null && args[i].startsWith(PROC_START_SEQ_IDENT)) {
                    startSeq = Long.parseLong(
                            args[i].substring(PROC_START_SEQ_IDENT.length()));
                }
            }
        }
        ActivityThread thread = new ActivityThread();
        thread.attach(false, startSeq);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }

        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

可以看到,它在第20行调用了Looper.prepareMainLooper()方法,我们继续源码跟踪,你会发现这个方法内部调用了Looper.prepare()方法,代码如下所示:

   public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }

因此我们应用程序的主线程中会始终存在一个Looper对象,从而不需要像子线程那样,我们再手动去调用Looper.prepare()方法了。

也就是说主线程内部已经自动调用了Looper.prepare()方法。这样基本就将Handler的创建过程完全搞明白了。

 

四、总结注意点:

1、从代码操作层面上来讲:在主线程中可以直接创建Handler对象,而在子线程中需要先调用Looper.prepare()才能创建Handler对象。

2、从源码解析上来说:创建Handler对象之前,必须拥有不为null的Looper对象。如果Looper对象为null,将会报错:"Can't create handler inside thread " + Thread.currentThread() + " that has not called Looper.prepare()"

3、每个线程只能创建一个 Looper对象

猜你喜欢

转载自blog.csdn.net/YuDBL/article/details/86684853