Android Looper vs BlockingQueue?
谁能解释为什么有人应该使用 Android Looper 功能来创建“管道线程”,而不是创建一个从 BlockingQueue 中提取任务的普通线程?从表面上看,这似乎是做同一件事的两种方法。
Can anyone explain why someone should use the Android Looper feature to create a "pipeline thread" instead of making a normal thread that pulls tasks from a BlockingQueue? On the surface, it seems like two ways to do the same thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BlockingQueue 可以让你有多个消费者和生产者,而 Looper 机制可以让你有多个生产者但只有一个消费者。
所以在Looper线程中你一次只执行一个任务(可运行)。 Looper 机制的创建是为了让您可以在 UI 线程(作为单线程运行,因此将其视为单线程使用者)上轻松执行可运行对象(封装为消息的任务)。
Looper/Handler 还提供延迟执行任务的功能,开箱即用的 BlockingQueue 则不然。这在 UI 工具包的上下文中也很重要。
BlockingQueue lets you have multiple consumers and producers whereas the Looper mechanism lets you have multiple producers but only one consumer.
So in Looper thread you only execute one task (runnable) at a time. The looper mechanism was created so you can easily executed runnables (tasks encapsulated as messages) on the UI thread (which runs as a single thread so think of it as a single thread consumer)
Looper/Handler also provide functionality for deferred exection of tasks which BlockingQueue out of the box doesn't. Again this is important in the context of UI toolkits.