AsyncTask进入等待状态 [英] AsyncTask goes into wait state

查看:73
本文介绍了AsyncTask进入等待状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定条件下取消异步任务. 我正在做以下事情:

I want to cancel the Async Task on the particular condition. I am doing the following stuff:

MyService.java

....
if(condition){

 asyncTask.cancel(true); // its return the true as well

}
...

MyAsynTask.java

...

protected Object doInBackground(Object... x) {
    while (/* condition */) {
      // work...
      if (isCancelled()){ // Here task goes in to wait state
 break;
  }
      else{
 //continue to download file

   }


    }
    return null;
 }
...

使用DDMS,我发现任务进入了等待状态.任何解决此问题的建议将不胜感激.

Using DDMS I found that task goes into wait State. Any suggestion to resolve this issue will be highly appreciated.

谢谢, Yuvi

推荐答案

AsyncTask是PoolExecutor的一项工作.当您执行第一个任务时,执行程序将创建第一个线程并在其上执行您的任务.任务执行完成后,不会删除线程.它开始等待新任务.

AsyncTask is a piece of work for PoolExecutor. When you execute your first task Executor creates first thread and executes your task on it. After task execution is finished the thread is not deleted. It starts waiting for a new task.

所以看到AsyncTask线程处于等待状态是正常的.

So it is normal to see AsyncTask thread in wait state.

P.S.最好不要使用AsyncTask进行长时间操作.使用您自己的执行程序或线程.

P.S. It's better not to use AsyncTask for longtime operation. Use your own executor or thread.

P.P.S.从4.x开始,AsyncTask使用单线程执行程序.小心

P.P.S. AsyncTask uses single thread executor since 4.x. Be careful )

这篇关于AsyncTask进入等待状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆