2

i'm having the following problem in android:

I need to process some asynchronous tasks in row (e.g. Async-Task 1 -> Async-Task 2 -> Asyc-Task 3). All of the async-tasks are answering with a handler (sendMessage(xy)).

So my handlers have to start the next async-task. Is this best practice? Should I prefer another way?

In Summary: I'm looking for a way, to process many asynchronous tasks serially...

I can't use the get()-function of an asyncThread, because they are returning their answers only in handlers.

Sorry for my bad english..

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Frame91
  • 3,670
  • 8
  • 45
  • 89

4 Answers4

2

Funny how both other answers had missed the fact you want to do it serially... :)

Personally I hate the AsyncTask, I would rather implement this in my own thread, but that is me.

You want Action A (and once completed) to invoke Action B (and so on)... which means you actually need a single thread to perform the sequence of BL actions.

This could easily be achieved by an object with two handlers, one your new ThreadHandler's Handler for the BL, and the other probably the UI thread Handler for UI updates, once a task is completed, it posts on the BL handler to start the next task.

This would encapsulate the process as a whole, and bugs would be easy to track. I've done this modeling twice for login flows with multiple servers & SMS validation codes, and few other stuff.

It would also cover a scenario where success or failure result of a task would require different tasks to be invoked.

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
  • do you mind to post some code on this implementation you are talking about? and +1 for actually noticing what the guy is asking even in his title. – Emil Adz Mar 25 '13 at 22:31
  • how about robospice?.http://stackoverflow.com/questions/15463146/is-using-asynctask-still-recommended-for-loading-listview-items-in-the-backgroun – Raghunandan Mar 25 '13 at 22:34
  • I don't know robospice, never needed anything to manage async tasks since I've learned very fast they hold memory leaks if not treated properly, and most of the time people don't, so I always had a pure java solution for all of this. – TacB0sS Mar 25 '13 at 22:51
  • Regarding an example, this would require a substantial implementation for it actually to work, I would not want to post pesado code which does not work properly, this is multi-threading we are talking about. and unfortunately I'm quite busy... :( – TacB0sS Mar 25 '13 at 22:53
  • Thanks for your reply! Can you explain in a few sentences, what you mean with an own thread-handler (what does BL stand for?)? That would be great! – Frame91 Mar 25 '13 at 23:38
  • 1
    create a new Thread Handler(), and call on its getHandler() method (to get the handler instance of the new thread so you may work with it). BL stands for BusinessLogic, whatever you want to perform in the background and not visibly e.g. get something from server, calculate something huge, wait for an SMS to arrive. – TacB0sS Mar 25 '13 at 23:42
  • From what i have searched on the net robopsice avoids memory leaks. can handle multiple request. notifies on the ui thread. Worth having a look at it. – Raghunandan Mar 26 '13 at 07:34
1

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.(Straight from the doc)

AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).(answer by commonware on a similar question)

http://developer.android.com/reference/android/os/AsyncTask.html.

http://developer.android.com/reference/java/util/concurrent/Executor.html. Consider using executor.

You can also consider using RoboSpice. https://github.com/octo-online/robospice You can also make multiple spicerequest.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

I recommend using a combination of Otto & Tape (from Square).

You can look at the sample which uploads images to a server using a queue.

https://github.com/square/tape/tree/master/tape-sample/src/main/java/com/squareup/tape/sample

dannyroa
  • 5,501
  • 6
  • 41
  • 59
0

If you need all these tasks to be executed at the same time then you really have no option but the launch multiple async tasks. However if you just need all these tasks to be executed in the background then you can probably just launch one async task and give it parameters to execute other tasks.

I would recommend looking into using Loaders instead of AsyncTasks. Loaders are run asynchronously and in my opinion just easier to manage in code. With the loaders when a loader finishes running an onLoaderFinished method is called, your Activity can implement the loader call backs and the onCreateLoader can handle multiple loaders so it all is a lot simpler than using AsyncTask. Here is a tutorial on loaders.

Ali
  • 12,354
  • 9
  • 54
  • 83