AsyncTasks 是否也在 SERIAL_EXECUTOR 中的同一后台线程上执行?

Are AsyncTasks also executed on the same background thread in the SERIAL_EXECUTOR?

关于 AsyncTasks 在 Honeycomb+ 上的执行顺序的问题。每 Google's documentation:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

我的问题是,在后台串行执行的AsyncTasks也会运行在同一个线程上吗?

例如,正在执行任务 A、B 和 C 的 AsyncTask.SERIAL_EXECUTOR,运行 线程 1 上的任务 A,然后是线程 1 上的任务 B,然后是线程 1 上的任务 C ?

或者,AsyncTask.SERIAL_EXECUTOR 是否有可能在线程 1 上执行任务 A,然后在线程 2 上执行任务 B,然后在线程 3 上执行任务 C?在此方案中,所有任务都按顺序执行,但它们在不同的后台线程上 运行。

我问是因为我在我的 doInBackground 中添加了一些调试代码并且我看到了名为 AsyncTask #5AsyncTask #4AsyncTask #2AsyncTask #3 的线程,当时我假设每次我只会看到一个名为 "AsyncTask Worker" 之类的线程。

(我只是想确认我自己对 AsyncTasks 预期工作方式的心理模型;我不是在报告这个问题中的错误或问题。)

谢谢!

will the AsyncTasks that are being executed serially in the background also be run on the same thread?

The implementation 很奇怪。 AsyncTask 没有使用 single-thread 线程池,而是维护自己的队列,并一次将任务馈送到常规 multiple-thread 线程池(可见的线程池 THREAD_POOL_EXECUTOR)。

is it possible that the AsyncTask.SERIAL_EXECUTOR will execute task A on thread 1, then task B on thread 2, then task C on on thread 3? In this scheme, all the tasks ARE executed serially, but they are being run on different background threads.

这是很有可能的,假设如果池目前未被使用,ThreadPoolExecutor 并不总是分发同一个线程。 Android 对 Java 框架 类 的实施比我平时更深入。另请注意,ThreadPoolExecutor 本身的实现在未来会发生变化,因为 Android 正在转向 OpenJDK 并远离他们的 Apache Harmony-based 框架 类。因此,我尽量不对 ThreadPoolExecutor 的实施方式做出任何假设。