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

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

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

class TaskHelper {public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {execute(task, (P[]) null);}@SuppressLint("NewApi")public static <P, T extends AsyncTask<P, ?, ?>> 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

android AsyncTask 只能在线程池里单个运行的问题相关推荐

  1. Android AsyncTask两种线程池分析和总结

    转自:http://bbs.51cto.com/thread-1114378-1-1.html Android AsyncTask两种线程池分析和总结 (一)    前言 在android Async ...

  2. Android之AsyncTask两种线程池分析和总结

    Android AsyncTask两种线程池分析和总结 (一)    前言 在android AsyncTask里面有两种线程池供我们调用 1.    THREAD_POOL_EXECUTOR, 异步 ...

  3. android代码里 写线程,在Android线程池里运行代码任务实例

    本节展示如何在线程池里执行任务.流程是,添加一个任务到线程池的工作队列,当有线程可用时(执行完其他任务,空闲,或者还没执行任务),ThreadPoolExecutor会从队列里取任务,并在线程里运行. ...

  4. 为什么线程池里的方法会执行两次_别以为线程池很简单,来回答下这些问题!...

    前言 线程池可以说是 Java 进阶必备的知识点了,也是面试中必备的考点,可能不少人看了这篇文章后能对线程池工作原理说上一二,但这还远远不够,如果碰到比较有经验的面试官再继续追问,很可能会被吊打,考虑 ...

  5. 为什么线程池里的方法会执行两次_面试官问你java都有哪些线程池,自己是否自定义过线程池...

    我还记得大学实习面试时,被问到什么是线程池这个问题,因为这个题我被录取了,原因就是我背出来了,而另外一个面试的没背出来,说实话当时还真不知道它是干什么的,就是看面试题给背下来了,在之后就是在实际开发中 ...

  6. 如何关闭线程池?会创建不会关闭?调用关闭方法时线程池里的线程如何反应?

    前言 相信大家在面试的时候经常会遇到「线程池」相关的问题,比如: 什么是线程池?线程池的优点? 有哪几种创建线程池的方式? 四种创建线程池的使用场景? 线程池的底层原理? 线程池相关的参数,比如Cor ...

  7. Android性能优化之线程池策略和对线程池的了解

    转载于 http://blog.csdn.net/roshen_android/article/details/52948480 线程的运行机制 1. 开启线程过多,会消耗cpu 2. 单核cpu,同 ...

  8. 为什么线程池里的方法会执行两次_新手一看就懂的线程池

    作者:码农田小齐 来源:https://www.cnblogs.com/nycsde/p/14003888.html 那相信大家也能感受到,其实用多线程是很麻烦的,包括线程的创建.销毁和调度等等,而且 ...

  9. java timer指定线程池_Java 定时器(Timer)及线程池里使用定时器实例代码

    java Timer定时器 简单实例代码:public class Test { public static void main(String[] args) { // Timer定时器 Timer ...

最新文章

  1. 平头哥发布一站式芯片设计平台“无剑”,芯片设计成本降低50%
  2. dedecms 漏洞_代码审计之二次漏洞审计
  3. ropgadgets与ret2syscall技术原理
  4. scala中zip拉链的操作
  5. Windows server 2008设置远程桌面
  6. Atitit 分布式之道 attilax著 第4章 通信 第7章 一致性和复制 第8章 容错性 第9章 安全性 第10章 基于对象的分布式系统 第11章 分布式文件系统 第12章 基于Web的分
  7. Windows Server 2012/2012R2 中配置 MSDTC,令其使用特定端口
  8. 盘点AI江湖中,清华人的“无问西东”
  9. pycharm免安装版推荐
  10. 计算机快速看图教程,CAD快速看图教程:CAD图纸测量方法集锦
  11. Java使用while循环计算调和数列的和并打印
  12. 机动车尾气排放智能抓拍解决应用方案
  13. ThingsBoard教程(九):前端架构分析
  14. 可折叠手机喂肥了黄牛,但柔性屏的未来从来不止手机
  15. 如何编辑制作并发送手机报?
  16. linux不支持modprobe命令,Linux中modprobe命令起什么作用呢?
  17. ModelSim 实用知识:优化,SDF,覆盖率
  18. 那些人尽可夫的男人啊——黄金圣斗士对同人女的真情告白2
  19. 学习笔记:Unity CustomSRP-5-Baked Light
  20. R语言sample随机抽样setseed固定随机数/真随机的原因和方法

热门文章

  1. 朴素贝叶斯算法+模型的评价-查准率、召回率、F1-score及混淆矩阵(code实现)
  2. pandas 字符串切片后保存_我擦~字符串转字节切片后,切片的容量竟然千奇百怪...
  3. 列名 userid 不明确。 表结构_SQL-Server(三)表的创建和操作
  4. php错误日志分析_php错误日志
  5. python画图turtle小人_python海龟绘图怎么增加每次画圆的半径|海龟python做图教程...
  6. Eclipse用法和技巧十五:自动添加未实现方法1
  7. Windows 8 Directx 开发学习笔记(十)纹理贴图实现旋转的木箱
  8. jmeter 聚合报告说明_Jmeter 测试结果分析之聚合报告简介
  9. docker-3-常用命令(下)
  10. oracle 体系结构及内存管理 15_存储结构