What is the sequence of executions for Asynctask?

Asked

Viewed 427 times

2

I need to synchronize data between the server and client application on Android and for this I created several classes ASyncTask, one synchronizes the Third Party, the other the Financial, Receivables and Payables, for example.

I did this to reuse code, which will be used in other services and activities.
However, the moment the user synchronizes the first time, on Activity as ASyncTask are executed one below the other, without checking whether the top has finalized the business rule.

My question is this, when a ASyncTask is finished soon the bottom one is executed or starts before the ASyncTask close the execution of?

The problem with this is occurring inconsistency in the Android database regarding the Foreign Keys for example when saving the records in Sqlite.

2 answers

7


Can you guarantee that the Asynctask’s are executed in sequence using the executeOnExecutor() to execute each of them.

This method takes as a parameter the Executioner which will execute Task’s, if you pass the value Asynctask.SERIAL_EXECUTOR they shall be executed sequentially.

ex:

asyncTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

// asyncTask2 só será executado após asyncTask1 ter terminado.
asyncTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

Edit:

Everything that was said earlier is true but this is from the version HONEYCOMB, the way the method execute() executes the tasks. That is, there is no need to use the method executeOnExecutor() if you want the tasks be executed in series.

Initially, when they were introduced, the Asynctasks were executed in series, in a single thread.
From the version DONUT this has been changed to a thread pool, allowing several tasks to be performed in parallel.
The emergence of several compatibility problems, in which old codes ceased to function properly, led to that from the version HONEYCOMB, the execution happens to be again in series.
At that time the method was introduced executeOnExecutor() to indicate explicitly how to implement.

  • I didn’t know about this appeal, it was pretty cool. My only caveat is that SERIAL_EXECUTOR is only one and database tasks can be serialized with others that have no logical relation if another part of the program uses the same resource. I would prefer to be explicit, making each step schedule the next task. Decreases the chance of a change far from affecting the functioning.

  • @epx I don’t understand what you mean. In practical terms the AsyncTask.SERIAL_EXECUTOR does the same as what you propose in your reply.

  • Let’s say it calls a method between scheduling task #1 and task #2. Then this method, which someone else did or will change, also uses the same trick to perform a handful of other serialized tasks.

3

If you schedule an Asynctask’s number, there is no enforcement order guarantee. To ensure this order you would have to schedule only the Asynctask you want to see executed first, and on onPostExecute() you schedule the second task, and so on.

  • What about the getStatus() method I can’t use in conjunction with Status.FINISHED, Status.RUNNING for example to check if Thread has been completed? Anyway I liked the way you passed it on to me and I had already thought about it, I wanted to see if there was a better way. Thank you for your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.