简体   繁体   English

android looper和executor线程池的区别

[英]difference between android looper and executor thread pool

我正在阅读有关 loopers以及Executor Thread Pools 的内容,它们似乎在做完全相同的事情……还是我错过了什么?

A Looper manages tasks that a Thread will run. Looper管理线程将运行的任务。 It puts them in a queue and then the Thread takes the next task in line.它将它们放在一个队列中,然后线程将执行下一个任务。 A Looper is tied to a specific Thread. Looper 绑定到一个特定的线程。

An Executor encapsulates managing and distributing tasks to different Threads.一个Executor封装了对不同线程的管理和分发任务。 If you have a fixed threadpool size of 1 then I suppose it would be similar in design to a Looper because it will just queue up the work for that one Thread.如果您的线程池大小固定为 1,那么我想它在设计上与 Looper 类似,因为它只会为那个线程排队工作。 If you have a threadpool with size > 1 then it will manage giving the task to the next Thread available to do the work, or in other words it will distribute tasks among all threads.如果您有一个大小 > 1 的线程池,那么它将管理将任务交给下一个可用的线程来完成工作,或者换句话说,它将在所有线程之间分配任务。

edit: Recommended reading: http://developer.android.com/reference/java/util/concurrent/package-summary.html编辑:推荐阅读: http : //developer.android.com/reference/java/util/concurrent/package-summary.html

Executors are more flexible.执行器更灵活。 For Android, the only time I really use Looper is when trying to make a Handler to communicate with the main thread from a background thread (which could even be in an ExecutorService).对于 Android,我真正使用 Looper 的唯一时间是尝试让 Handler 从后台线程(甚至可以在 ExecutorService 中)与主线程进行通信。 For example:例如:

Handler mainThreadHandler = new Handler(Looper.getMainLooper());
mainThreadHandler.post(new Runnable...); //runs on main thread

Let me add that android looper can be used by native code.让我补充一点,本机代码可以使用 android looper。 The Android Looper system is made up of the Looper class, Handler class, MesseageQueue class. Android Looper系统由Looper类、Handler类、MesseageQueue类组成。 One looper is bounded to one thread.一个循环器绑定到一个线程。 From Andorid 4.0, MessageQueue is implemented by both java code and c code, which are connected.从 Andorid 4.0 开始,MessageQueue 由 java 代码和 c 代码实现,两者相连。 You can send a message to the same MessageQueue via native code or java code.您可以通过本机代码或 Java 代码向同一个 MessageQueue 发送消息。

So the difference are:所以区别在于:

  1. Looper is simple with one thread, however ExecutorThreadPool is complicated and flexible with one or more threads. Looper 用一个线程很简单,但是 ExecutorThreadPool 用一个或多个线程复杂和灵活。

  2. Looper can be conveniently used by native code.本地代码可以方便地使用 Looper。

Besides, Looper and Handler is commonly used in Android code.此外,Looper 和 Handler 常用于 Android 代码中。 Some android developers are more farmilar with Handler than ExecutorThreadPool.一些 android 开发者比 ExecutorThreadPool 更熟悉 Handler。

It might be important to note that AndroidX defines HandlerExecutor .需要注意的是,AndroidX 定义了HandlerExecutor Same class is available from GMS. GMS 提供相同的课程。 This is an executor that uses a Handler that can be built on any looper.这是一个执行程序,它使用可以构建在任何 Looper 上的 Handler。 For example, this way we can get an Executor for Main thread on API level < 28 .例如,通过这种方式,我们可以在 API 级别 < 28 上为 Main 线程获取一个 Executor

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

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