asyncTask 的execute和executeOnExecutor 方法

简介:

asyncTask.execute

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


 看了这篇文章,http://blog.csdn.net/hitlion2008/article/details/7983449, 讲的挺好

  更多解释:http://blog.csdn.net/guolin_blog/article/details/11711405

   http://www.cnblogs.com/fotransit/archive/2013/04/17/3025937.html


    本文转自 一点点征服   博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/6844198.html,如需转载请自行联系原作者


相关文章
|
7月前
|
Java 测试技术
线程池execute和submit区别
线程池execute和submit区别
99 1
final, finally, finalize的区别。
final, finally, finalize的区别。
128 0
Callable中call方法和Runnable中run方法的区别
Callable中call方法和Runnable中run方法的区别
183 0
Callable中call方法和Runnable中run方法的区别
|
存储 安全 Java
final 、finally finalize 有什么不同?
final 、finally finalize 有什么不同?
218 0
|
Java 数据库连接
final 、finally、finalize有什么区别
final 、finally、finalize有什么区别
152 0
|
Java Android开发
AsyncTask
http://developer.android.youdaxue.com/reference/android/os/AsyncTask.html public abstract class AsyncTask extends Object java.lang.Object android.os.AsyncTask<Params, Progress, Result>
162 0
|
Java API 调度
【Android 异步操作】AsyncTask 异步任务 ( AsyncTask 异步任务执行方法 execute 方法相关源码解析 )
【Android 异步操作】AsyncTask 异步任务 ( AsyncTask 异步任务执行方法 execute 方法相关源码解析 )
174 0
|
Java Android开发
【Android 异步操作】AsyncTask 异步任务 ( FutureTask 模拟 AsyncTask 执行过程 | AsyncTask 执行过程回顾 | FutureTask 分析 )
【Android 异步操作】AsyncTask 异步任务 ( FutureTask 模拟 AsyncTask 执行过程 | AsyncTask 执行过程回顾 | FutureTask 分析 )
199 0