使用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)
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的功能,但重复制造轮子的苦力还是没有成就感。。。
AsyncTask时异常: Cannot execute task: the task has already been executed (a task can be executed only o
原文:http://blog.csdn.net/sunalongl/article/details/22864869