AsyncTask execute()或executeOnExecutor()吗? [英] AsyncTask execute() or executeOnExecutor()?

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

问题描述

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

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

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

> 16的新SDK应该使用什么?

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.

应用于> 16的新SDK(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提供的各种API.包,例如Executor,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天全站免登陆