Log activity for synchronization capturing task data running in background

Asked

Viewed 29 times

1

I develop an application in Cordova, which synchronizes with an online database, the synchronization part is all in Java. (Context of the app)

I am developing a "Log" screen for the synchronization of which will be shown to the user how many records were successfully synchronized, how many with error and the respective errors.

Through researches I have come to the conclusion that I should use a Thread to be "listening" to synchronization.

The question is as follows: I have seen in the documentation that Asynctask should be used for tasks of the order of a few seconds of execution and for longer tasks use Executor, Threadpoolexecutor and Futuretask. Synchronization usually takes up to hours at first sync. Should I actually use the second option? If someone has a good tutorial that shows me how to do it because what I already researched I can not apply to my problem.

Why Asynctask cannot be used for time-consuming tasks?

1 answer

0

The Asynctasks, by default, work as a shared queue. All tasks are executed serial, one at a time, by a thread in the background.

Does that mean that if you have a task that takes too long to complete, other tasks that are eventually queued will have to wait to be performed.

If you have a guarantee that other tasks will not be queued when your sync happens, or that these queues will not cause problems, you can use without problems.

Otherwise, you can make Asynctask work in parallel. Just run it with the method executeOnExecutor(java.util.concurrent.Executor, Object[]), passing a Executor different from the standard. Asynctask already offers AsyncTask.THREAD_POOL_EXECUTOR, or you can create your.

Note that this can bring parallelism bugs due to indefinite running sequence


An example of the use of a Executor or ThreadPoolExecutor you can consult the source of Asynctask itself, since she also uses this mechanism to perform her tasks.

Browser other questions tagged

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