0

I am trying to optimize a complex data updater and parser for my Android app. The server provides three interface functions. The parser requires the data from all those three functions.

When the download of the data is finished, the parser can start. It consists of many different independent tasks which can be parallelized.

I was thinking of using Futures or FutureTasks for processing the data.

So basically, this is the procedure:

  1. create Task-1, Task-2, Task-3 for downloading the data
  2. wait for the downloads to be finished
  3. create Task-1,..., Task-N for parsing the data
  4. wait for the parser to be finished
  5. call a callback to signal that process is done.

My first question: is it possible to create Futures with asynchronous functions, which use callbacks to return the data (network framework)?

Second question: are there any drawbacks in using Futures or FutureTasks respectively in this scenario or are there any better solutions to achieve that?

Thank you.

Denis Loh
  • 2,194
  • 2
  • 24
  • 38
  • I accomplished the issue above by using a CountDownLatch for the depending tasks. I will post an answer soon. – Denis Loh Jan 29 '15 at 15:05

1 Answers1

0

Basically you are Trying to achieve the following.

Step 1 - User from UI starts 1,2,... n download tasks. Step 2 - Once each of the task is completed, new thread should be started to process it. Step 3 - Once all n Tasks are completed, UI should be updated ... may be with a success dialog.

This can be achieved easily by using Async Task. I am going to tell you the approach and not the code sample.

Things to note about Async Task

Before 1.6, Async Task handles all background operations with a single additional thread. After 1.6 till 3.0 .. it was changed, so that a pool of thread had begun to be used. And operations could be processed simultaneously.

Since Honeycomb default behavior is switched back to use of a single worker thread (one by one processing).

How to implement your requirement

For your requirement, you can use the method (executeOnExecutor) to run simultaneous tasks (1 till n tasks) if you wish (there two different standard executors: SERIAL_EXECUTOR and THREAD_POOL_EXECUTOR).

The way how tasks are enqueued also depends on what executor you use. In case of a parallel one you are restricted with a limit of 10 (new LinkedBlockingQueue(10)). In case of a serial one you are not limited (new ArrayDeque()).

So the way your tasks are processed depends on how you run them and what SDK version you run them on. As for thread limits, we are not guaranteed with any, yet looking at the ICS source code we can say that number of threads in the pool can vary in range 5..128.

When you start too many tasks (like 100s or more) with default execute method serial executor is used. Since tasks that cannot be processed immediately are enqueued you get OutOfMemoryError (thousands of tasks are added to the array backed queue).

Exact number of tasks you can start at once depends on the memory class of the device you are running on and, again, on executor you use.

So by following this approach, once all the tasks are completed, you can use a handler to update the UI.

Hope this helps.

Prem
  • 4,823
  • 4
  • 31
  • 63
  • But how do I detect that all tasks are actually done? What I was looking for is a some kind of gate, which is released when all results are available. I know how to use AsyncTaks to queue multiple Jobs in parallel but not how to sync their results. – Denis Loh Jan 07 '15 at 06:30