博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android AsyncTask 只能在线程池里单个运行的问题
阅读量:5115 次
发布时间:2019-06-13

本文共 3059 字,大约阅读时间需要 10 分钟。

android 的AysncTask直接调用Execute会在在一个线程池里按调用的先后顺序依次执行。

如果应用的所有网络获取都依赖这个来做,当有一个网络请求柱塞,就导致其它请求也柱塞了。

在3.0 以后引入了新的方法。可以不在一个线程池里运行。

class TaskHelper {    public static 
> void execute(T task) { execute(task, (P[]) null); } @SuppressLint("NewApi") public static
> void execute(T task, P... params) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); } else { task.execute(params); } }}

 

asyncTask.execute

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with android.os.Build.VERSION_CODES.DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. After android.os.Build.VERSION_CODES.HONEYCOMB, it is planned to change this back to a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use. 

This method must be invoked on the UI thread.必须UI线程中调用

注意:这个函数让任务是以单线程队列方式或线程池队列方式运行,依赖于平台版本而有所不同。asyncTask首次引入时,这个函数会让任务以后台单线程串行方式执行。从android.os.Build.VERSION_CODES.DONUT(android 1.6)开始,它让允许任务在线程池中多任务并行执行。但在 android.os.Build.VERSION_CODES.HONEYCOMB(android 3.0)之后,它又该回去了,变成了单线程执行的模式,原因是多线程并行执行容易引发问题。如果你真想并行执行任务,你可以使用另外一个版本:使用THREAD_POOL_EXECUTOR参数的executeOnExecutor方法,但要注意使用警告提示

anyncTask.executeOnExecutor

This method is typically used with THREAD_POOL_EXECUTOR to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor for custom behavior. 
Warning: Allowing multiple tasks to run in parallel from a thread pool is generally not what one wants, because the order of their operation is not defined. For example, if these tasks are used to modify any state in common (such as writing a file due to a button click), there are no guarantees on the order of the modifications. Without careful work it is possible in rare cases for the newer version of the data to be over-written by an older one, leading to obscure data loss and stability issues. Such changes are best executed in serial; to guarantee such work is serialized regardless of platform version you can use this function with SERIAL_EXECUTOR. 
This method must be invoked on the UI thread.
Parameters:
exec The executor to use. THREAD_POOL_EXECUTOR is available as a convenient process-wide thread pool for tasks that are loosely coupled.

这个方法通常和THREAD_POOL_EXECUTOR一起使用,允许多个任务在由AsyncTask管理的线程池中并行执行,但是您你也可以使用自定义行为的Executor。

 

警告:因为执行操作顺序并未定义,通常情况下,允许多个任务在线程池中并行执行,其结果并非是你想要的。例如:这些任务都要去修改某个状态值(诸如点击按钮写文件),因为没有确定的修改顺序,旧的修改可能会覆盖新修改的版本内容,导致不稳定数据丢失而变成一个稳定的问题。因此这种任务最好是串行执行;确保这些任务串行执行而不依赖于平台版本的方法是,使用SERIAL_EXECUTOR

 

转载于:https://www.cnblogs.com/likwo/p/4225640.html

你可能感兴趣的文章
ssRs父子维度
查看>>
关押罪犯
查看>>
SUSE Labs Con 2018有感
查看>>
Jquery基础概括
查看>>
Competitive
查看>>
php视图操作
查看>>
Nexus6P 设置Debug模式
查看>>
扩展BSGS-传送门
查看>>
bzoj1588 营业额统计
查看>>
Java:一步步带你深入了解神秘的Java反射机制
查看>>
HCNA配置浮动静态路由
查看>>
构造完全图
查看>>
linux单机配置DG过程记录
查看>>
剑指offer(61)序列化二叉树
查看>>
但愿Fedora 10有Ubuntu Wubi安置遵命
查看>>
计算机中的CPU
查看>>
windows server2012在已有.net4.5框架的基础上安装.net3.5的方法
查看>>
python day10
查看>>
python-函数
查看>>
VMWare安装Ubuntu 12.04开启虚拟机的Unity Mode模式
查看>>