问题
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