先来看下官方对AsyncTask 的解释:
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.)
AsyncTask被设计成一个助手类线程和处理程序和不构成通用线程框架。asynctask理想情况下应该用于短操作(最多几秒钟)。
If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent
package such as Executor
, ThreadPoolExecutor
and FutureTask
.
如果你需要保持线程运行很长一段时间,强烈建议使用java.util.concurrent 中的API,如Executor
, ThreadPoolExecutor
and FutureTask。
Android2.3以前的版本,也即SDK/API 10和以前的版本
内部的线程池限制是5个,也就是说同时只能有5个线程运行,超过的线程只能等待,等待前面的线程某个执行完了才被调度和运行。换句话说,如果一个进程中的AsyncTask实例个数超过5个,那么假如前5个都运行很长时间的话,那么第6个只能等待机会了。这是AsyncTask的一个限制,而且对于2.3以前的版本无法解决。如果你的应用需要大量的后台线程去执行任务,那么你只能放弃使用AsyncTask,自己创建线程池来管理Thread,或者干脆不用线程池直接使用Thread也无妨。不得不说,虽然AsyncTask较Thread使用起来比较方便,但是它最多只能同时运行5个线程,这也大大局限了它的实力,你必须要小心的设计你的应用,错开使用AsyncTask的时间,尽力做到分时,或者保证数量不会大于5个
从Android 3.0开始对AsyncTask的API做出了一些调整:
#execute()提交的任务,按先后顺序每次只运行一个也就是说它是按提交的次序,每次只启动一个线程执行一个任务,完成之后再执行第二个任务,也就是相当于只有一个后台线程在执行所提交的任务(Executors.newSingleThreadPool())。
public static void execute (Runnable runnable)
Convenience version of execute(Object)
for use with a simple Runnable object. See execute(Object[])
for more information on the order of execution.
public final AsyncTask<Params, Progress, Result> executeOnExecutor (Executor exec, Params... params)
Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.
This method is typically used with THREAD_POOL_EXECUTOR
to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor
for custom behavior.
Warning: Allowing multiple tasks to run in parallel from a thread pool is generally not what one wants, because the order of their operation is not defined. For example, if these tasks are used to modify any state in common (such as writing a file due to a button click), there are no guarantees on the order of the modifications. Without careful work it is possible in rare cases for the newer version of the data to be over-written by an older one, leading to obscure data loss and stability issues. Such changes are best executed in serial; to guarantee such work is serialized regardless of platform version you can use this function with SERIAL_EXECUTOR
.
This method must be invoked on the UI thread.
Parameters
exec | The executor to use. THREAD_POOL_EXECUTOR is available as a convenient process-wide thread pool for tasks that are loosely coupled. |
---|---|
params | The parameters of the task. |
Returns
- This instance of AsyncTask.
Throws
IllegalStateException | If getStatus() returns either RUNNING or FINISHED . |
---|
See Also
Fields
public static final Executor SERIAL_EXECUTOR
An Executor
that executes tasks one at a time in serial order. This serialization is global to a particular process.
public static final Executor THREAD_POOL_EXECUTOR
An Executor
that can be used to execute tasks in parallel.
。