Manikandan Kuppusamy
Manikandan Kuppusamy

Reputation: 21

What happens if same AsyncTask is called multiple times with different parameter in Android

I have a question about AsyncTask. If I am calling the same AsyncTask using different string values like this

 next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              new MyAsyncTask().execute("Android");
new MyAsyncTask().execute("Java");
new MyAsyncTask().execute("Hadoop");
new MyAsyncTask().execute("Python");
new MyAsyncTask().execute("Dot Net");
new MyAsyncTask().execute("Hibernate");
        }
    });

I am calling the same AsyncTask with different values using a single button click. Now my question is, what will happen in the above code? Will all the AsyncTasks execute in parallel or sequentially? If they execute in parallel how? If they execute sequentially how? I want a clear explanation. If any one knows please tell me.

Thanks in advance.

Upvotes: 0

Views: 830

Answers (1)

Chetan Joshi
Chetan Joshi

Reputation: 5711

AsyncTask uses a thread pool pattern for running the stuff from doInBackground(). The issue is initially (in early Android OS versions) the pool size was just 1, meaning no parallel computations for a bunch of AsyncTasks. But later they fixed that and now the size is 5, so at most 5 AsyncTasks can run simultaneously.

Find More details in below link:

[Running multiple AsyncTasks at the same time — not possible?

Multiple AsyncTask In Android ]2

Thread Pool Pattern

AsyncTask uses a thread pool pattern for running the stuff from doInBackground()

The Thread pool Pattern is where number of Threads are created to perform a number of Tasks. It is basically a container where multiple threads come in a queue for different task.

Multiple AsyncTasks In Android

In earlier version of Android (Donut and below), multiple AsyncTasks execution were not allowed. The pool size was just 1. But after Donut (1.6) it has been relaxed and now the size is 5, so at most 5 AsyncTasks can run simultaneously.

Limitation Of AsyncTask

There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with max number of worker threads (128) and the delayed tasks queue has fixed size 10. If you try to execute more than 138 AsyncTasks the app will crash with java.util.concurrent.RejectedExecutionException.

Delayed Queue Size

AsyncTasks use a fixed size queue internally for storing delayed tasks. Queue size is 10 by default. If you start 15 AsyncTasks in a row, then first 5 will enter their doInBackground(), but the rest will wait in a queue for a free worker thread. As soon as any of the first 5 AsyncTasks finishes, and thus releases a worker thread, a task from the queue will start execution. So in this case at most 5 tasks will run simultaneously. However if you start 16 your custom tasks in a row, then first 5 will enter their doInBackground(), the rest 10 will get into the queue, but for the 16th, a new worker thread will be created so it’ll start execution immediately. So in this case at most 6 tasks will run simultaneously.

Upvotes: 1

Related Questions