为什么Looper.loop()不阻止UI线程

时间:2019-11-14 07:57:12

标签: android handler android-looper

这是ActivityThread.main()中的代码:

    public static void main(String[] args) {

        ......

        Looper.prepareMainLooper();

        ...

        Looper.loop();

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

这使Looper得以运行。 Looper.loop()一直都在运行一个循环。为什么Looper.loop()不阻止UI线程?

1 个答案:

答案 0 :(得分:1)

Looper.loop()准备Looper来运行传递到线程的消息。

它不会盲目地对其自身进行迭代,而是使用MessageQueue来监听消息并运行它们。

这是一种事件驱动的方法,其中LooperMessageQueue通知它包含消息时准备循环并运行消息。进一步了解 android事件循环有效,请看一下此articleLooper.looop()source