API 级别 11 之前的 AsyncTask.executeOnExecutor() [英] AsyncTask.executeOnExecutor() before API Level 11

查看:22
本文介绍了API 级别 11 之前的 AsyncTask.executeOnExecutor()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Android 中执行 AsyncTask 的正常方式是,来自 Android API:

The normal way we do AsyncTask in Android is, from Android API:

 private class DoIntenseTask extends AsyncTask<Object, Object, Void> {
   protected Void doInBackground(Object... params) {
     for (Object param : params) {
         Object rtnObj = doIntenseJob(param);
         publishProgress(rtnObj);
     }
     return null;
   }

   protected void onProgressUpdate(Object... progress) {
     for (Object rtnObj : progress) {
       updateActivityUI(rtnObj);
     }
   }

 }

我的密集任务是松散耦合的,执行顺序无关紧要,通过这种方式,分配了一个线程来运行密集任务列表.我个人认为这是一种半途而废的解决方案.是的,密集作业不再在UI线程中运行,但仍需要一个一个执行(很多情况下,我们面临着密集作业列表,我想这也是AsyncTask中的方法是多参数化的原因).Google 应该使 API 更具可重用性,以解决不同类型的场景.

My intense tasks are loosely coupled and the execution order does not matter, by doing this way, a single thread is allocated to run a list of intense tasks. personally I think this is a sort of halfway solution. Yes, the intense job is not running in UI thread anymore, but still need execute one by one (in many cases, we are facing a list of intense job, I think this is also why the methods in AsyncTask are multi-parameterized). Google should make the API more reusable to solve different kind of scenario.

我真正喜欢的是并行运行多个由线程池管理的 doIntenseJob()(例如 poolSize = 5).看起来谷歌确实通过 AsyncTask.executeOnExecutor() 提供了一个解决方案,但不幸的是只从 API 级别 11 开始可用.我正在移动设备上开发应用程序,想知道是否有一种解决方法可以在 API 级别 11 下实现相同的行为.

What I really like to have is run a number of doIntenseJob() in parallel managed by a threadpool (e.g. poolSize = 5). Looks like google do give a solution by AsyncTask.executeOnExecutor() but unfortunately only available since API level 11. I am developing app on mobile and wonder if there is a workaround that I can achieve the same behavior under API level 11.

提前致谢

推荐答案

我的密集任务是松散耦合的,执行顺序无关紧要,通过这种方式,分配了一个线程来运行密集任务列表.

My intense tasks are loosely coupled and the execution order does not matter, by doing this way, a single thread is allocated to run a list of intense tasks.

AsyncTask 目前使用具有多个线程的线程池.未来可能会限制在单线程中——谷歌暗示会是这样.

AsyncTask presently uses a thread pool with several threads. In the future, it may be restricted to a single thread -- Google has hinted that this will be the case.

想知道是否有解决方法可以在 API 级别 11 下实现相同的行为.

wonder if there is a workaround that I can achieve the same behavior under API level 11.

默认行为是您想要的行为.如果您检查 源代码以AsyncTask,你会看到从 Gingerbread 开始,它使用了一个线程池,最少 5 个线程,最多 128 个.

The default behavior is the behavior you want. If you examine the source code to AsyncTask, you will see that as of Gingerbread, it used a thread pool with a minimum of 5 threads and a maximum of 128.

现在,请记住,当今使用的绝大多数 Android 设备都是单核的.因此,除非您的密集任务"不是做太多事情而是阻塞网络 I/O,否则您不希望并行执行它们,因为线程之间的上下文切换只会进一步减慢您的速度.

Now, bear in mind that the vast majority of Android devices in use today are single-core. Hence, unless your "intense tasks" are intensely not doing much but blocking on network I/O, you do not want to be doing them in parallel, as context switches between threads will simply slow you down further.

这篇关于API 级别 11 之前的 AsyncTask.executeOnExecutor()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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