为什么Looper.loop()不阻止UI线程 [英] Why does Looper.loop() not block the UI thread
问题描述
这是ActivityThread.main()中的代码:
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");
}
这使Looper得以运行.Looper.loop()一直都在运行一个循环.为什么Looper.loop()不阻止UI线程?
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?
推荐答案
Looper.loop()
准备 Looper
来运行传递给线程的消息.
Looper.loop()
prepares the Looper
to run the messages that are passed to the thread.
它不会盲目地对其自身进行迭代,而是使用 MessageQueue
来监听消息并运行它们.
It doesn't blindly keep iterating over itself, rather it uses a MessageQueue
to listen for messages and runs them.
这是一种事件驱动的方法,当 MessageQueue
通知它包含消息时, Looper
准备循环并运行消息.进一步了解android事件循环有效,请看以下
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.loop()不阻止UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!