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

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

问题描述

我们做的AsyncTask在Android上正常的方法是,从Android的API:

 私有类DoIntenseTask扩展的AsyncTask<对象,对象,太虚> {
   保护无效doInBackground(对象... PARAMS){
     对(目标参数:PARAMS){
         对象rtnObj = doIntenseJob(参数);
         publishProgress(rtnObj);
     }
     返回null;
   }

   保护无效onProgressUpdate(对象...进度){
     对于(对象rtnObj:前进){
       updateActivityUI(rtnObj);
     }
   }

 }
 

我的激烈的任务是松散耦合的执行顺序并不重要,通过这样的方式,单个线程被分配给运行的激烈任务列表。我个人认为这是一种中途解决方案。是的,强烈的作业不会在UI线程中运行了,但还需要一个执行一个(在很多情况下,我们正面临着激烈的就业名单,我想这也是为什么在AsyncTask的方法是多参数)。谷歌应该使API更可重复使用,以解决不同类型的场景。

我真的很想有运行一些doIntenseJob()并行的一个线程池管理(如poolSize = 5)。看起来像谷歌这样做,因为给API级别11.我正在开发移动和奇迹的应用程序通过AsyncTask.executeOnExecutor(解决方案),但不幸的是唯一可用的,如果有一个解决方法,我能实现在API级别相同的行为11.

提前
谢谢 是

解决方案
  

我的激烈的任务是松散耦合的执行顺序并不重要,通过这样的方式,单个线程被分配给运行的激烈任务列表。

的AsyncTask presently使用几个线程的线程池。在将来,它可能被限制为单个线程 - 谷歌已经暗示这将是这种情况

  

不知道是否有一种变通方法,我可以实现在API级别11。

相同的行为

默认行为是你想要的行为。如果检查<一href="http://www.google.com/$c$csearch#cZwlSNS7aEw/frameworks/base/core/java/android/os/AsyncTask.java">the来源$ C ​​$ C到的AsyncTask ,你会认为这是姜饼,它使用一个线程池用最少5个线程和最多128个。

现在,请记住,在广阔的大多数目前使用的Andr​​oid设备都是单核。因此,除非你的紧张工作​​都在紧张没有做多少,但阻塞网络I / O,你不想做他们的并行,线程之间的上下文切换只会进一步下降减缓你。

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);
     }
   }

 }

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.

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.

Thanks in advance
Y

解决方案

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 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.

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

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.

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.

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

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