AsyncTask execute() 还是 executeOnExecutor()? [英] AsyncTask execute() or executeOnExecutor()?

查看:15
本文介绍了AsyncTask execute() 还是 executeOnExecutor()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 execute()executeOnExecuter() 有什么区别?

What's the difference between using execute() and executeOnExecuter()?

  • execute() 默认是如何执行任务的?(串行还是并行?)

  • How does execute() execute tasks by default? (in serial or in parallel?)

新 SDK > 16 应该使用什么?

What should be used for new SDKs >16?

对任务使用并行执行(THREAD_POOL_EXECUTOR)而不是串行是否是一个好习惯,即使这对应用程序无关紧要,或者取决于AsyncTask 将被执行?

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 AsyncTasks that will be executed?

推荐答案

.execute 如何默认执行任务(串行或并行).

How does .execute execute tasks by default (in serial or in parallel).

API 级别 11 之前:并行.

Before API level 11: parallel.

API 级别 11 及以上:串行.

API level 11 and up: serial.

哪些应该用于新的 SDK >16 (executeOnExecuter ?)

which should be used for new SDKs >16 (executeOnExecuter ?)

取决于您的要求.如果您对默认执行程序感到满意,请使用 execute().如果不是,请使用显式执行器.

Depends on your requirements. Use execute() if you're happy with the default executor. Use an explicit executor if you're not.

对任务使用并行执行 (THREAD_POOL_EXECUTOR) 而不是串行是否是一个好习惯,即使这对应用程序无关紧要,或者这取决于将执行的异步任务的数量?

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 文档:

Async tasks should only be used for relative short backround operations. Quoting AsyncTask documentation:

AsyncTasks 最好用于短期操作(最多几秒钟).如果您需要让线程长时间运行,强烈建议您使用 java.util.concurrent 提供的各种 APIExecutor、ThreadPoolExecutor 和 FutureTask 等包.

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.

在异步任务运行期间,执行器线程无法执行其他任务.在只有一个执行程序线程的串行执行程序上,当您的任务运行时间过长时,更容易检测到问题.在检测此类问题的并行执行器上,需要更多同时进行的长时间运行的任务.

While the async task is running, the executor thread cannot execute other tasks. On a serial executor with only one executor thread it is easier to detect problems when your tasks run for too long. On a parallel executor detecting such problems takes more simultaneous long-running tasks.

因此,如果您确实需要切换到并行执行器,最好重新审视您的设计.

Therefore, if you really need to switch to a parallel executor, you're probably better off revisiting your design.

这篇关于AsyncTask execute() 还是 executeOnExecutor()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆