Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask execute() or executeOnExecutor()?

What's the difference between using execute() and executeOnExecuter()?

  • How does execute() execute tasks by default? (in serial or in parallel?)

  • What should be used for new SDKs >16?

  • Is it a good practice to use parallel execution (THREAD_POOL_EXECUTOR) for tasks rather than serial even if it doesn't matter for the application or does that depends on the number of AsyncTasks that will be executed?

like image 650
Rashad.Z Avatar asked Apr 29 '15 07:04

Rashad.Z


People also ask

What happens if we call Execute () more than once in Async task?

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.

In which thread AsyncTask function will execute?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What can I use instead of AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

Is AsyncTask deprecated?

AsyncTask deprecated alternative Android – Java private final Executor executor = Executors. newSingleThreadExecutor(); A Handler gives you a mechanism to push tasks into the UI thread queue from any other threads thus allowing other threads to communicate with the UI thread.


2 Answers

.execute() - this function schedules the task on a queue for a single background thread. Means that if you are calling two AsyncTasks and using .execute() method to call them, they will execute in a queue(first then second).

.executeOnExecutor() - If you want parallel execution of both AsyncTasks, you can use this method for execution of AsyncTask. Means both asyncTasks will execute simultaneously.

In simple words: .execute() method creates a single thread for execution of asyncTasks, and .executeOnExecuter() method creates separate thread for each ayncTask.

.execute executes tasks by default in serial order.

EDITED: If you want to use executeOnExecutor() you can use this code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);  else      task.execute(); 

Before HONEYCOMB execute() method run AsynkTask in parallel.

like image 181
Rahul Sharma Avatar answered Oct 02 '22 19:10

Rahul Sharma


How does .execute execute tasks by default (in serial or in parallel).

Before API level 11: parallel.

API level 11 and up: serial.

which should be used for new SDKs >16 (executeOnExecuter ?)

Depends on your requirements. Use execute() if you're happy with the default executor. Use an explicit executor if you're not.

Is it a good practice to use parallel execution (THREAD_POOL_EXECUTOR) for tasks rather than serial even if it doesn't matter for the application or does that depends on the number of async tasks that will be executed?

Async tasks should only be used for relative short backround operations. Quoting AsyncTask documentation:

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

While the async task is running, the executor thread cannot execute other tasks. On a serial executor with only one executor thread it is easier to detect problems when your tasks run for too long. On a parallel executor detecting such problems takes more simultaneous long-running tasks.

Therefore, if you really need to switch to a parallel executor, you're probably better off revisiting your design.

like image 38
laalto Avatar answered Oct 02 '22 19:10

laalto