Android 4.0 asynctask 同时不可能

标签 android multithreading android-asynctask android-4.0-ice-cream-sandwich

我有同样的问题Running multiple AsyncTasks at the same time -- not possible? 除了我使用 android 4.0 和 android:minSdkVersion="14"。

我试过他的例子,也得到了:

bar bar bar
bar bar bar
bar bar bar

编辑:

我找到了解决方案 here

而不是使用:

task.execute();

使用:

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

最佳答案

听起来像这样解释(来自 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.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

关于Android 4.0 asynctask 同时不可能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8718402/

相关文章:

java - AsyncTask 更好的通信协议(protocol)

android - AsyncTask.execute() 在调用 onDestroy 后运行回调

android - 您可以在 Jetpack Compose 中使用 TopAppBar 设置 NavController 吗?

Android 应用程序的 Java 服务器

java - Haskell 中的 Android 应用程序

每次我想要获取登录信息时,JavaFX 任务都会将状态更改为失败

编译器相关 - 这两个 C 代码真的相同吗?

java - 在JAVA中,当守护线程中满足某些条件时,我们可以让主方法等待( sleep )一段时间吗?

java - 如何传递参数并从 AsyncTask 类获取结果?

android - 无法使用 LiveParse 查询合并 dex