AsyncTask时异常: Cannot execute task: the task has already been executed (a task can be executed only o

使用AsyncTask时有一个破异常,这个异常让我从AsyncTask转到Handler

 Cannot execute task: the task has already been executed (a task can be executed only once)

看官方文档,提示:

Open Declaration AsyncTask<List<BitmapPosition>, BitmapPosition, Void> android.os.AsyncTask.execute(List<BitmapPosition>... params)


Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it. 

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. Starting android.os.Build.VERSION_CODES.HONEYCOMB, tasks are back to being executed on 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.

Parameters:
params The parameters of the task.
Returns:
This instance of AsyncTask.
Throws:
IllegalStateException - If getStatus() returns either AsyncTask.Status.RUNNING or AsyncTask.Status.FINISHED.
See Also:
executeOnExecutor(java.util.concurrent.Executor, Object[])
execute(Runnable)

意思是说execute一次只能执行一个任务,不然就做报 IllegalStateException异常,当你想一次执行多个任务时,用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. 

就是说,由于多任务并行运行没有顺序,所以不一定是你想要的结果。

但我既然已经到了这一步,也就一直用下去,改好之后,发现还有版本限制,娘的,最讨厌这个了,一下让我能支持的最低版本
由8改到11,虽然现在2.3用户相当少了,但作为一个苦逼的程序员, 这还是无法忍受了,无可奈何,只有又改回execute,将代码继续优化,恶心了一把,以后尽量用handler吧,无限制,自己控制,虽说可以自己定义回调实现AsyncTask的功能,但重复制造轮子的苦力还是没有成就感。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值