Java Android 两个 AsyncTask 串行执行还是并行执行?- 第二个是冻结,但结果还可以

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18661288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:57:24  来源:igfitidea点击:

Android two AsyncTasks serially or parallel execution? - The second is freezing but the result is ok

javaandroidandroid-asynctaskprogress-bar

提问by George Melidis

I run two AsyncTask tasks in my Android application which are from the same class but with different parameters. For example:

我在我的 Android 应用程序中运行了两个 AsyncTask 任务,它们来自同一类但具有不同的参数。例如:

new myAsynckTask(a,b,c).execute();
new myAssyncTask(a,d,e).execute();

Do they execute in parallel or in a serial order? I ask this because when the first one starts, shows the progress of execution and when finishes I see the second one which needs more time to finish but I can't see the progress(I'm able to see the rectangle but the progress bar is not showing 20%..and so on). It's like freezing but the result is ok.

它们是并行执行还是串行执行?我问这个是因为当第一个开始时,显示执行进度,当完成时我看到第二个需要更多时间才能完成但我看不到进度(我可以看到矩形但进度条没有显示 20% ......等等)。这就像冷冻,但结果还可以。

What I want to do is to run them in serial order and be able to see the progress in the two of them. I run the app on Android Jelly Bean 4.2.2 API Level 17

我想要做的是按顺序运行它们,并且能够看到它们两个的进度。我在 Android Jelly Bean 4.2.2 API 级别 17 上运行该应用程序

回答by CommonsWare

Do they execute in parallel or in a serial order?

它们是并行执行还是串行执行?

If your android:targetSdkVersionis 13 or higher, and you are running on an Android 3.2 or higher device, they will be executed serially.

如果您android:targetSdkVersion是 13 或更高版本,并且您在 Android 3.2 或更高版本的设备上运行,它们将被串行执行。

If you are running on Android 1.5, they will be executed serially.

如果您在 Android 1.5 上运行,它们将被串行执行。

Otherwise, they will be executed in parallel.

否则,它们将并行执行。

You can opt into parallel execution by replacing execute()with executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).

您可以通过替换选择加入并行执行execute()executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

For more, see the "Order of Execution" section of the AsyncTaskJavaDocs.

如需更多信息,请参阅的“执行顺序”一节AsyncTask的JavaDocs

回答by Cruceo

The answer to your question is: it totally depends on what version of Android you're running this on, and is a huge problem I've faced in several applications.

你的问题的答案是:这完全取决于你运行它的 Android 版本,这是我在几个应用程序中遇到的一个巨大问题。

You should check out this linkif you want to see how to run them correctly

如果您想了解如何正确运行它们,您应该查看此链接

回答by luckyhandler

UPDATE: copied from Android Developersand initiated by Yazazzello

更新:从Android Developers复制并由 Yazazzello 发起

"This class was deprecated in API level 26.0.0-alpha1. Use AsyncTask directly."

“此类在 API 级别 26.0.0-alpha1 中已弃用。直接使用 AsyncTask。”

You should use this for parallel execution:

您应该将其用于并行执行:

AsyncTaskCompat.executeParallel(new AsyncTask<Param, Void, Data>() {
                @Override
                protected Data doInBackground(Param... params) {
                    return downloader.getData(params[0]);
                }

                @Override
                protected void onPostExecute(Data response) {
                    processData(response);
                }
}, param);