I'm following this tutorial to create an XML reader with options to download multiple feeds at once. Are there any downsides to executing multiple AsyncTasks (max 3) simultaneously? I'm going to add a timer to check if they finished.
Running multiple AsyncTasks at the same time -- not possible? It is possible. However it could be, that due to the cpu depending on each device, one is faster than the other. But as #Alex already answered, you wont get "real" multitasking. Haven't it tried yet I would assume, that doing it all in one AsyncTask is faster. And you could reuse the connection you established to the server.
For better architecture I'll choose 1 AsyncTask per request. It is easier to manage actual request. Also it would be easier to modify (add/remove request).
Downsides of executing multiple AsyncTasks depend on your knowledge of AsyncTask lifecycle. You need to execute it correctly (depends on android version).
Good article about the dark side of Async tasks http://bon-app-etit.blogspot.com.by/2013/04/the-dark-side-of-asynctask.html
They won't be run simultaneously, but serially. See here:
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
Related
I'm using the BoltsFramework (Parse) in an android Application. Let's say I want to launch in background several Parallel Tasks (so several Threads). Is there a limit to avoid to use too much Threads? Or can we put in a queue if the tasks are too many? I would like to avoid call the tasks in series.
The use case should be something like launch several task in parallel (huge number) and when All are completed do something….
Bolts-Android uses a thread pool with a queue behind the scenes, so you don't have to worry about managing it yourself. If you want to see how it's implemented, you can see BoltsExecutors.java and AndroidExecutors.java.
In a JVM environment, it'll use the default Executors.newCachedThreadPool() which has a limit of Integer.MAX_INT simultaneous parallel threads and a synchronous queue when it runs out of threads.
In an Android environment, it uses a custom pool size that sizes itself depending on how many cores your CPU has and has a synchronous queue when it runs out of threads as well.
You can also pass in your own Executor in several methods to manage the thread pool yourself.
https://github.com/BoltsFramework/Bolts-Android/blob/master/bolts-tasks/src/main/java/bolts/BoltsExecutors.java
https://github.com/BoltsFramework/Bolts-Android/blob/master/bolts-tasks/src/main/java/bolts/AndroidExecutors.java
android Asynctask has been modified quite frequently between different API-levels. I'm developing an Application in which i've to upload images to FTP server. i want to do that in serialized order (images upload after one-another by one image upload per asyntask). I understand the SERIAL_EXECUTOR and THREAD_POOL_EXECUTOR stuff, but i just want some clarity about what is the default behavior of asynctask ( my min. target API is ICS 4.0 ). if i simply execute say 10 asyncs' in a loop, will they go to thread queue and execute one by one or they'll just go parallel ?
Look in the AsyncTask documentation:
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.
So, with min target of 14, they will be serialized.
You can't use one async task with loop inside doInBackground()? If you want to have control over them, you can invoke second async task in onPostExecute() of first.
I am offloading many background operations (like web access, and DB operations) to AsyncTasks in order to make my application run faster. It partially works, because the UI does not hang. But it still takes the app a long time to settle (i.e. finish the background tasks).
When looking at the logcat I noticed that all the background tasks (AsyncTask) run one after the other. No concurrency. How can one write a truly multi-threaded application?
Reading the AsyncTask's documentation you can see that indeed Android runs all non-main-thread tasks in a single thread. See "order of execution" in http://developer.android.com/reference/android/os/AsyncTask.html
But there you also have the solution: use executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
I have already developed an Activity which will parse JSON data and display the results in a ListView. I am using an AsyncTask for this purpose.
What I want now is that, when I click on an item in the ListView, the file should start downloading. Can I write another AsyncTask in the same activity so that this AsyncTask will do the downloading work for me? Is there any problem with having multiple AsyncTasks in the same activity?
As per from the Doc yes you can.
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.
BEst answer How is itself on stackoverflow.
There shouldn't be any problem with multiple Asynctasks in a single activity. You should be careful to clearly define the values that each one manipulates (for example, if task B relies on a value given by task A, make sure that A must finish first), but in general, it should be fine. I have a project right now with three asynctasks running upon first install, and it's ticking along fine thus far.
Yes.. You can.
AsyncTask is simple thread handler implementation.
I have an Android application which has the main UI thread, and several other worker threads which I have spawned using ASyncTask.
However, there is a scenario that I can produce every time in which I attempt to spawn a new thread with ASyncTask using task.execute(), and it does not call doInBackground(). The thread never seems to start. Then my application seems to spin for a little while, and then begins to hang.
Here are the threads I am using:
And here is the memory usage on the device:
It does not look as though it is failing to spawn due to memory issues.
Is there some other underlying reason that I do not know of? Maximum number of threads? Is there any way for me to find out why it is not executing?
AsyncTask uses a ThreadPoolExecutor used internally with a core pool size of 5 and a LinkedBlockingQueue. In simpler terms: you can have atmost 5 AsyncTasks active at the same time. Additional tasks will be queued till one of the other AsyncTasks does not return from doInBackground().
You may want to review your code to free up some AsyncTasks. If thats not possible, you can try to create a CustomAsyncTask class in your project based on the original AsyncTask code which can be found here. Try setting the CORE_POOL_SIZE variable to a higher value or using a SynchronousQueue.
Did you read the docs?
Note: this function schedules the task on a queue for a single
background thread or pool of threads depending on the platform
version. 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. After
HONEYCOMB, it is planned to change this back to a single thread to
avoid common application errors caused by parallel execution. If you
truly want parallel execution, you can use the
executeOnExecutor(Executor, Params...) version of this method with
THREAD_POOL_EXECUTOR; however, see commentary there for warnings on
its use.
Source