您好,所有关于在Android中执行多个asynctask并行操作我都感到困惑
在Donut之前,它只是单一处理,这意味着我们一次只能执行一个任务,但是后来又改变了,我们可以并行执行多个asynctask,在这种情况下,执行多个asynctask的限制也只能执行138 Async Task线程在其他情况下会通过异常,但后来蜂窝发生了变化,我们可以一次执行5个asynctask,等待队列中可以执行10个,但是如果我们一次执行15个以上asynctask,则意味着我是否要执行一次执行16个任务,然后它将并行执行6个任务,而其中10个将处于队列中,这意味着等待队列中将至少有10个任务等待工作线程完成,而后来在Kitkat中,它已经进行了更改并且首先获取Vm中当前正在运行的处理器数量,并据此执行多个asynctask。
现在我的问题是:
请回答我的问题。
最佳答案
引用Android开发人员的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.) 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.
内部
AsyncTask
是通过Executor
实现的,默认值是SerialExecutor
。限制== CPU核心数的意义在于,如果限制更大,则启动的线程之间将一直存在竞争。您知道,一天结束时,线程可以同时工作。在许多情况下,线程之间的竞争不是问题,但在某些情况下是这样。我猜Google只是认为优点不如缺点,这就是为什么他们再次返回
SerialExecutor
的原因。再次引用AsyncTask
类描述:Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
这些问题主要是
InterruptedException
和IllegalStateException
。最后,如果您真的知道自己在做什么,并且有充分的理由要覆盖Google的默认行为,则可以通过使用
AsyncTask.executeOnExecutor(Executor, Object[])
并传入THREAD_POOL_EXECUTOR
来实现。
关于java - Android并行执行多个AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30011054/