How to kill previous tasks when calling a new asynchronous task

I am using asynctask to fetch images from a given url.these images are displayed in the listview.the problem is when I scroll through the listview quickly app crashing.logcat error

03-27 13:08:42.355: E/AndroidRuntime(25567): java.util.concurrent.RejectedExecutionException: pool=128/128, queue=10/10

      

I learned from this error that it is a problem of running many asynchronous threads concurrently. To solve this problem, I wrote the code below.

   NWTaskObj obj = new NWTaskObj();
            obj.setType(AdapterType.OfferAdapter);
            obj.setLink(offer.getImageLink());
            obj.setObject(this);
            if(count<10){
            new HandleOfferImage().execute(obj); 
            }else{
            new HandleOfferImage().cancel(true);
            new HandleOfferImage().execute(obj); 
            }

      

count is a static variable .it is incremented in the HandleOfferImage class. But it still crashes in the same scenario. I need to kill the previous asynctasks (if count> 10) except for the last one, but one. I don't want to use a lazy list here. Can anyone help me?

+3


source to share


6 answers


If you have multiple tasks and want to cancel all tasks, it's very easy. Just store all the async tasks you run in the ArrayList. And when you want to cancel these tasks, just iterate over this ArrayList and cancel all the tasks. Just. How:



HandleOfferImage myAsyncTask = new HandleOfferImage();
aList.add(myAsyncTask); //aList is you array list
myAsyncTask.execute(obj);

//When you want to cancel
for(HandleOfferImage task : aList) {
    task.cancel();
}

      

0


source


new HandleOfferImage().cancel(true);

      

wrong. hold the task pointer in variale as mTask and then

mTask.cancel(true);

      



UPDATE:

if (mTask!= null) {
    mTask.cancel(true);
}

mTask= new TaskGetTeams();
mTask.execute();

      

+1


source


Maintain a boolean variable of type isloggedin in the Application class. Make true isloggedin where you want to start Task and false isloggedin where you want to stop the task. Check this isloggedin flag for each level of your problem. Do your task if your isloggedin ytrue flag only fails your task otherwise and exits the task.

0


source


Also, you need to check doBackground () whether the async will be revoked or not.

From the developer's site.

Canceling a task

The task can be canceled at any time by calling cancel (boolean). Calling this method will result in subsequent calls to isCancelled () to return true. After calling this method, onCancelled (Object) instead of onPostExecute (Object) will be called after doInBackground (Object []) returns. To ensure that the task is canceled as quickly as possible, you should always check the return value of isCancelled () periodically from doInBackground (Object []), if possible (inside a loop, eg.)

0


source


Taking a boolean variable.

boolean stopTask = false,isDoing = true;

      

Suppose this is your AsyncTask

public class HandleOfferImage extends AsyncTask
{
      public void doInBackGround()
      {
           if(stopTask==false)
           {
               isDoing = true;
               // do your stuff
           }
           else
           {
               isDoing = false;
           }

      }
}

      

now in your code,

if(count<10){      
            stopTask = true; // stop task
            while(isDoing==true){} // wait to stop
            stopTask = false;
            new HandleOfferImage().execute(obj);       
            }else{      

            new HandleOfferImage().cancel(true);      
            stopTask = true;
            while(isDoing==true){} // wait to stop
            stopTask = false;

            new HandleOfferImage().execute(obj);       
            }      

      

This is what I suppose might help you, but I haven't tested this code myself.

0


source


 if(count<maxConst){
                task=new HandleOfferImage();
                offerImageTask.add(task);
                task.execute(obj);
            }else{ 
                for(int nCount=0;nCount<offerImageTask.size()-2;nCount++){
                offerImageTask.get(nCount).cancel(true);
                offerImageTask.remove(nCount);

                }
                task=new HandleOfferImage();
                offerImageTask.add(task);
                task.execute(obj);
            } 

      

here maxConst = 10 and offerImageTask is an arraylist of the HandleOfferImage class.

0


source







All Articles