/**     * Executes the task with the specified parameters. The task returns     * itself (this) so that the caller can keep a reference to it.     *     * <p>This method is typically used with {@link #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 {@link Executor} for custom     * behavior.     *     * <p><em>Warning:</em> Allowing multiple tasks to run in parallel from     * a thread pool is generally <em>not</em> 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 {@link #SERIAL_EXECUTOR}.     *     * <p>This method must be invoked on the UI thread.     *     * @param exec The executor to use.  {@link #THREAD_POOL_EXECUTOR} is available as a     *              convenient process-wide thread pool for tasks that are loosely coupled.     * @param params The parameters of the task.     *     * @return This instance of AsyncTask.     *     * @throws IllegalStateException If {@link #getStatus()} returns either     *         {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.     *     * @see #execute(Object[])     */    public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,            Params... params) {}


From: http://blog.csdn.net/lichao3459/article/details/17712393

当我执行两次调用asyncTask.execute(task);时发现只有当第一次的任务完成后才执行下一下任务!!怎么回事?

AyncTask不是号称异步线程池吗?既然是线程池那么多任务执行时应该可以并发执行啊,至少两个任务可以并发执

行,以前看过一个视频,人家的就可以啊!纠结了一下午,通过查阅资料和自己的动手实验终于把问题搞明白了。

原来在SDK3.0以前的版本执行asyncTask.execute(task);时的确是多线程并发执行的,线程池大小为5,最大可大

128个,google在3.0以后的版本中做了修改,将asyncTask.execute(task);修改为了顺序执行,即只有当一个的实

的任务完成后在执行下一个实例的任务。

那么怎么才能并发执行呢,很简单,3.0后新增了一个方法executeOnExecutor(Executor exec, Object... params),

该方法接受2个参数,第一个是Executor,第二个是任务参数。第一个是线程池实例,google为我们预定义了两种:

一种是AsyncTask.SERIAL_EXECUTOR,第二种是AsyncTask.THREAD_POOL_EXECUTOR,顾名思义,第一

其实就像3.0以后的execute方法,是顺序执行的。第二种就是3.0以前的execute方法,是可以并发执行的。我们直

接用asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, task);就可以多任务并发执行了。

既然executeOnExecutor第一个参数是Executor,那么我们可以自定义Executor吗?当然可以,Executor主要由四

类型newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

(具体使用解析可以看我上一篇文章点击打开链接),可是当我这样使用

asyncTask.executeOnExecutor(Executors.newFixedThreadPool(1), task);或者

asyncTask.executeOnExecutor(Executors.newSingleThreadExecutor, task);并没有像我想象的与

asyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, task);那样是单线程顺序执行,而是像

asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, task);是多线程并发执行的,我不是

已经规定newFixedThreadPool的线程池数量是1或者是newSingleThreadExecutor单线程了么!怎么回事呢?原来程

序在每次调用asyncTask.executeOnExecutor(Executors.newFixedThreadPool(2), task)时会获取一个新的Executor对

象,这个对象内的线程只执行对应的task,所以无论哪种情况每个task都有一个新的线程来执行,即并发执行。

知道原因就好办了,我们定义个一全局静态变量

private static ExecutorService exec = Executors.newSingleThreadExecutor();程序在每次调用

asyncTask.executeOnExecutor(exec, task);时是使用的同一个Executor,执行效果如下:

当Executor类型为:private static ExecutorService exec = Executors.newFixedThreadPool(2);只有两个线程在执行

任务

当Executor类型为:private static ExecutorService exec = Executors.newSingleThreadExecutor();只有一个线程在执行任务


更多相关文章

  1. Android关于在Canvas类里的绘制线程问题汇总
  2. Android如何保证一个线程最多只能有一个Looper?
  3. 秒杀多线程第二篇 多线程第一次亲密接触 CreateThread与_beginth
  4. android 之 View
  5. Android的生命周期中的方法的执行场景
  6. Android获取所有存储卡挂载路径
  7. Android的消息处理机制(Looper,Handler,Message)
  8. Android(安卓)JNI 篇 - JNI回调的三种方法(精华篇 ndk)
  9. Android(安卓)7.0 Launcher3的启动和加载流程分析----转载

随机推荐

  1. Android消息机制全面解析
  2. Android下uid与多用户释疑(一)
  3. android 触摸事件、点击事件的区别
  4. Android图表
  5. Android系统编译―Android.mk文件的简单
  6. Android(安卓)Push Notification技术实现
  7. [zz] Android(安卓)初始化语言(Android(安
  8. 利用HTML5开发Android笔记(中篇)
  9. Android应用程序进程启动过程的源代码分
  10. Android(安卓)事件捕捉和处理流程分析