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.
Related
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
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 am retrieving already created data in all user controls. And there are 3 Spinners so I am using SERIAL_EXECUTOR to execute it one by one. But now, issue is that, one Spinner retrieves data based on another Spinner.
So which AsyncTask executor should I use for that Spinner by which It can load its data although SERIAL_EXECUTOR is going on.
In short, I want to know which AsyncTask should I use which will execute that Task even though SERIAL_EXECUTOR is in process.
If you want to execute an AsyncTask parallel to another one then you can use THREAD_POOL_EXECUTOR like this:
AsyncTask task = new SomeAsyncTask();
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
But I warn you that this might not work as good as you might like. If the AsyncTasks you are executing perform quite a bit of work on their own than executing them in parallel will slow them down considerably. If possible, try to execute tasks serially, only execute in parallel if you absolutely have to. If you need to load data which is dependent on some other kind of data consider doing it in one big AsyncTask instead of splitting the work to to AsyncTasks. Always try to keep the amount of AsyncTasks or Threads to a minimum. The fewer you have the faster your app will load.
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