Creating another AsyncTask inside of doInBackground(在 doInBackground 中创建另一个 AsyncTask)
问题描述
我最近浏览了 SO 以找到相同问题的答案,但是没有针对这样做的风险的问题的答案.但基本上我想在另一个 AsyncTask
的 doInBackground()
方法中运行另一个 AsyncTask
.这是一个不好的方法和/或它是否会留下任何潜在的副作用?
I recently looked through SO to find the answer to the same question here, but there was not an answer directed at the question in regards to the risk of doing so. But basically I want to run another AsyncTask
inside the doInBackground()
method of another AsyncTask
. Is this a bad approach and/or does it leave any potential side effects?
我知道在 onPostExecute()
中运行它是有效的,到目前为止,由于 onPostExecute()
运行回来,我没有遇到任何问题在启动 AsyncTask
开始的主线程上.
I know that running it in the onPostExecute()
works and so far from past experiences I have not had any issues due to the fact that onPostExecute()
runs back on the main thread which started a AsyncTask
to begin with.
推荐答案
来自 API 文档:
•任务实例必须在 UI 线程上创建.
•The task instance must be created on the UI thread.
doInBackground() 在后台线程上运行.所以你不能从 doInBackground() 创建和运行另一个异步任务.
doInBackground() runs on the background thread. So you cannot create and run another asynctask from doInBackground().
http://developer.android.com/reference/android/os/AsyncTask.查看线程规则下的主题.
执行异步任务时,任务会经过 4 个步骤:(直接来自文档)
When an asynchronous task is executed, the task goes through 4 steps: (Straight from the doc)
1.onPreExecute(),在任务执行前在UI线程上调用.此步骤通常用于设置任务,例如通过在用户界面中显示进度条.
1.onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
2.doInBackground(Params...),在 onPreExecute() 执行完毕后立即在后台线程上调用.此步骤用于执行可能需要很长时间的后台计算.异步任务的参数传递到这一步.计算的结果必须由这一步返回,并传回上一步.这一步也可以使用publishProgress(Progress...)来发布一个或多个进度单元.这些值发布在 UI 线程的 onProgressUpdate(Progress...) 步骤中.
2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
3.onProgressUpdate(Progress...),在调用publishProgress(Progress...)后在UI线程上调用.执行的时间是不确定的.此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度.例如,它可用于为进度条设置动画或在文本字段中显示日志.
3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
4.onPostExecute(Result),后台计算完成后在UI线程上调用.后台计算的结果作为参数传递给这一步.
4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
首次引入时,AsyncTasks 在单个后台线程上串行执行.从 DONUT 开始,这已更改为允许多个任务并行操作的线程池.从HONEYCOMB开始,任务在单线程上执行,避免并行执行导致的常见应用错误.
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.
如果您确实想要并行执行,可以使用 THREAD_POOL_EXECUTOR 调用 executeOnExecutor(java.util.concurrent.Executor, Object[]).
您也可以考虑使用替代 RoboSpice.https://github.com/octo-online/robospice.
可以提出多个香料请求.任务完成时 ui 线程上的通知.值得一看 robospice.
Can make multiple spice request. Notitifes on the ui thread when task is complete. Worth having a look at robospice.
这篇关于在 doInBackground 中创建另一个 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 doInBackground 中创建另一个 AsyncTask
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01