-3

I have a problem that I cant understand it at all!. I have an Asynctask that connects to a web service and download some data.

But when I do this my other Asynctasks waits to this Asynctask to finish his work evenAsynctasksin otherActivities`!!. But I can't understand this.

How I can avoid this?. I was thinking that Asynctasks can run simultaneously but now it doesn't!. What is the problem? It seems REST and downloading stream data blocks the entire app.

4

2 回答 2

5

如果您在 Android 3.0 或更高版本上运行,并且您使用的是AsyncTask(即框架中的那个)的默认实现,它一次只会执行一个任务。但是,可以并行执行。见下文。

来自AsyncTask 的文档

执行顺序

首次引入时,AsyncTasks它们在单个后台线程上串行执行。从 开始DONUT,这被更改为允许多个任务并行运行的线程池。从 开始HONEYCOMB,任务在单个线程上执行,以避免并行执行导致的常见应用程序错误。

如果你真的想要并行执行,你可以调用executeOnExecutor(java.util.concurrent.Executor, Object[])with THREAD_POOL_EXECUTOR

于 2013-09-23T19:03:24.260 回答
2

用一个executoOnExecutor

TheTask task = new TheTask();
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); 

http://developer.android.com/reference/java/util/concurrent/Executor.html

于 2013-09-23T19:03:38.377 回答