I read a post about asynctask here that says to execute it in parellel with main thread use executeOnExecutor method of this class. I thought asynctask to already run in parellel. Can someone explain this for me?
-
See: http://stackoverflow.com/a/24788257/295004 – Morrison Chang Nov 18 '15 at 03:54
2 Answers
Thanks to @Rookie and @Nashe
From what I understood, AsyncTask can run serially and in parallel. When AsyncTask run in serial, each task in executed one after the other ie. suppose you use AsyncTask to decode image from file and you want to decode multiple images for your listview. Then you need to execute multiple AsyncTask on adapter's getView() and each of these AsyncTask are executed serially ie. one after the other.
But as newer devices have multiple physical cores, these device can execute multiple thread at the same time. In this case each of your AsyncTask can run in parallel to one another.
But you cannot predict which of these thread will complete first. ie. your 5th row of listview may have finished decoding image before your first row.
So AsyncTask.executeOnExecutor()(since API 11) lets you execute AsyncTask in parallel and AsyncTask.execute() lets you execute AsyncTask serially
I hope it helps you..

- 1,701
- 1
- 16
- 18
-
According to the link shared by Morrison Chang it says that after api 11 asynctask is made to run serially as it was made parellely by default after donut. So your statement AsyncTask before API 11 ran serially is not correct. But It gives me good idea about my confusion. thnx – Rookie Nov 18 '15 at 04:11
-
First you should understand where we will use executeOnExecutor method, AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
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 example if you want to play a song and download a file simultaneously at that time you have to use asynctask parallel execution(ASYNCTASK.THREAD_POOL_EXECUTOR)

- 2,767
- 7
- 44
- 84
-
Thanks for your answer. But I have one doubt as when I am playing a song I will use seprate asynctask and when downloading a file I'll use another. Won't these two be run in seperate threads? – Rookie Nov 18 '15 at 04:21
-
A thread pool can run multiple parallel instances of a task http://codetheory.in/android-java-executor-framework/ – skyshine Nov 18 '15 at 04:28
-
Ya it surely do but my question is when I am doing two different tasks on two different asynctasks, do I need to execute them in parellel or they itself will run in parellel. What I understand is that when we are using a same asynctask implementation then we need to make it run parellel as the same async implementation waits for the one task to complete before jumping on another. But when we are using different asynctasks then there is no need to make them run parellel as they are different and they will certainly run in different instances of asynctasks. Correct me if I am wrong. @suresh – Rookie Nov 18 '15 at 04:36
-
if you are not using executor they won't run parellel,if you use executor they itself run in parellel – skyshine Nov 18 '15 at 04:59