Rookie
Rookie

Reputation: 8790

Issue with multiple async tasks

I am starting 6 asyncTasks at a time. But my problem is that how do I know that which async task's onPostExecute() is returned.

Upvotes: 0

Views: 776

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

For more Information. http://developer.android.com/reference/android/os/AsyncTask.html

public final AsyncTask<Params, Progress, Result> executeOnExecutor (Executor exec, Params... params)

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

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.

This method must be invoked on the UI thread.

Upvotes: 1

Andrii Chernenko
Andrii Chernenko

Reputation: 10194

IMO the best solution in this case is to pass some kind on callback to each of your AsyncTasks. Here's how it's done:

  1. Create your callback interface:

    public interface OnTaskCompleteListener() {
        public void onTaskComplete(Void result);
    }
    
  2. Then add this callback as constructor parameter of your AsyncTask:

    public class MyAsyncTask extends AsyncTask { 
        private OnTaskCompleteListener listener; 
    
        public MyAsyncTask(OnTaskCompleteListener listener){ 
            this.listener=listener; 
        }
    
    
        @Override 
        protected void onPostExecute(Void result) {
            if (this.listener!=null) {
                this.listener.onTaskComplete(result);
            }
        }
    }
    
  3. And, finally, when instantiating your AsyncTask, provide callback implementation for each instance:

    //instance 1
    new MyAsyncTask(new OnTaskCompleteListener() {
        public void onTaskComplete(Void result) {
            //do something with the result
        }
    }).execute();
    
    //instance 2
    new MyAsyncTask(new OnTaskCompleteListener() {
        public void onTaskComplete(Void result) {
            //do something completely different here
            //this code doesn't interfere in any way with instance 1
        }
    }).execute();
    

Notice that you are not obligated to implement callback for each AsyncTask, implementations can be shared.

Upvotes: 2

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

myTask extends AsyncTask{ 
int mTag; 
public AsyncTas(int tag){ 
this.mTag = tag; 
}


@Override protected void onPostExecute(Void arg0) {
super.onPostExecute(arg0); 
switch(tag){ .... } 

}

}

Upvotes: 1

Related Questions