1

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?

Yt100
  • 324
  • 4
  • 12
  • Does this answer your question? [Why main thread's Looper.loop() doesn't block UI thread?](https://stackoverflow.com/questions/35931899/why-main-threads-looper-loop-doesnt-block-ui-thread) – Jakir Hossain Nov 14 '19 at 08:01
  • No. I saw it before post the question. – Yt100 Nov 14 '19 at 08:06

1 Answers1

4

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. Looper.looop()

(source)

greybeard
  • 2,249
  • 8
  • 30
  • 66
Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24