AsyncTask execute() 还是 executeOnExecutor()?
AsyncTask execute() or executeOnExecutor()?
使用execute()
和executeOnExecuter()
有什么区别?
execute()
默认情况下如何执行任务? (串行还是并行?)
新 SDK >16 应该使用什么?
对任务使用并行执行(THREAD_POOL_EXECUTOR
)而不是串行执行是否是一种好习惯,即使它对应用程序无关紧要,或者这取决于 AsyncTask
将要执行的?
.execute() - 此函数为单个后台线程安排队列中的任务。意味着如果您调用两个 AsyncTasks 并使用 .execute() 方法调用它们,它们将在队列中执行(首先然后是第二个)。
.executeOnExecutor() - 如果你想并行执行两个AsyncTasks,你可以使用这个方法来执行AsyncTask。意味着两个 asyncTasks 将同时执行。
简单来说:.execute() 方法创建一个线程来执行 asyncTasks,.executeOnExecuter() 方法为每个 ayncTask 创建单独的线程。
.execute 默认按顺序执行任务。
已编辑:
如果你想使用 executeOnExecutor() 你可以使用这个代码:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
task.execute();
在 HONEYCOMB execute() 方法之前 运行 AsynkTask 并行。
How does .execute execute tasks by default (in serial or in parallel).
在 API 级别 11 之前:并行。
API 11 级及以上:连续。
which should be used for new SDKs >16 (executeOnExecuter ?)
取决于您的要求。如果您对默认执行程序感到满意,请使用 execute()
。如果不是,请使用显式执行程序。
Is it a good practice to use parallel execution (THREAD_POOL_EXECUTOR) for tasks rather than serial even if it doesn't matter for the application or does that depends on the number of async tasks that will be executed?
异步任务应该只用于相对较短的后台操作。引用 AsyncTask
文档:
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.
当异步任务运行ning时,执行线程不能执行其他任务。在只有一个执行程序线程的串行执行程序上,当您的任务 运行 时间太长时,更容易检测到问题。在并行执行器上检测此类问题需要同时进行更多长时间 运行ning 任务。
因此,如果您真的需要切换到并行执行器,您最好重新审视您的设计。
使用execute()
和executeOnExecuter()
有什么区别?
execute()
默认情况下如何执行任务? (串行还是并行?)新 SDK >16 应该使用什么?
对任务使用并行执行(
THREAD_POOL_EXECUTOR
)而不是串行执行是否是一种好习惯,即使它对应用程序无关紧要,或者这取决于AsyncTask
将要执行的?
.execute() - 此函数为单个后台线程安排队列中的任务。意味着如果您调用两个 AsyncTasks 并使用 .execute() 方法调用它们,它们将在队列中执行(首先然后是第二个)。
.executeOnExecutor() - 如果你想并行执行两个AsyncTasks,你可以使用这个方法来执行AsyncTask。意味着两个 asyncTasks 将同时执行。
简单来说:.execute() 方法创建一个线程来执行 asyncTasks,.executeOnExecuter() 方法为每个 ayncTask 创建单独的线程。
.execute 默认按顺序执行任务。
已编辑: 如果你想使用 executeOnExecutor() 你可以使用这个代码:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
task.execute();
在 HONEYCOMB execute() 方法之前 运行 AsynkTask 并行。
How does .execute execute tasks by default (in serial or in parallel).
在 API 级别 11 之前:并行。
API 11 级及以上:连续。
which should be used for new SDKs >16 (executeOnExecuter ?)
取决于您的要求。如果您对默认执行程序感到满意,请使用 execute()
。如果不是,请使用显式执行程序。
Is it a good practice to use parallel execution (THREAD_POOL_EXECUTOR) for tasks rather than serial even if it doesn't matter for the application or does that depends on the number of async tasks that will be executed?
异步任务应该只用于相对较短的后台操作。引用 AsyncTask
文档:
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.
当异步任务运行ning时,执行线程不能执行其他任务。在只有一个执行程序线程的串行执行程序上,当您的任务 运行 时间太长时,更容易检测到问题。在并行执行器上检测此类问题需要同时进行更多长时间 运行ning 任务。
因此,如果您真的需要切换到并行执行器,您最好重新审视您的设计。