Why does Looper.loop() not block the UI thread

不想你离开。 提交于 2021-02-10 14:29:10

问题


This is the code in ActivityThread.main():

    public static void main(String[] args) {

        ......

        Looper.prepareMainLooper();

        ...

        Looper.loop();

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

It made the Looper running. There is a loop running in the Looper.loop() all the time. Why does Looper.loop() not block the UI thread?


回答1:


Looper.loop() prepares the Looper to run the messages that are passed to the thread.

It doesn't blindly keep iterating over itself, rather it uses a MessageQueue to listen for messages and runs them.

It's an event driven methodology, where the Looper is prepared to loop and run the messages when the MessageQueue notifies it that it contains messages. To know more about how android event loop works, take a look at this article.

(source)



来源:https://stackoverflow.com/questions/58851888/why-does-looper-loop-not-block-the-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!