简体   繁体   English

Looper / Handler与执行器

[英]Looper/Handler vs. Executor

Why use Looper/Handler when I could use an Executor method(s)? 当我可以使用Executor方法时,为什么还要使用Looper / Handler?

The Looper/Handler duo seems rather clunky and doesn't seem to do a great deal beyond allowing the queuing of runnables and seemingly has less flexibility. Looper / Handler二重奏似乎很笨拙,除了允许可运行对象排队外,似乎没有做太多事情,而且灵活性似乎较低。

What was the looper design rationale? 弯针的设计原理是什么?

Thanks. 谢谢。

A looper by itself does not have many advantages over an executor. 与执行程序相比,循环程序本身没有很多优点。 It is how Android manages worker. 这就是Android管理工作人员的方式。 But you are able to get the main thread of your app with Looper.getMainLooper() , this can have the following advantages: 但是您可以使用Looper.getMainLooper()获得应用程序的主线程,这可以具有以下优点:

  • You can change the ui from the MainLooper, but also it is NOT compute intense background task, since you would freeze the ui. 您可以从MainLooper更改ui,但是由于冻结ui,因此它不是计算密集型后台任务。
  • Execute Runnables "in-sync" with the all the other native tasks. 与所有其他本机任务“同步”执行Runnable。 Meaning you post your Runnable in the same queue as android does 意味着您将Runnable与android放在同一队列中
  • No need to create a thread yourself. 无需自己创建线程。 You can use the already running Looper. 您可以使用已经运行的Looper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM