Log activity for synchronization capturing task data running in background

1

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

I'm developing a "Log" screen for synchronization which will show the user how many records have been successfully synchronized, how many with error and their errors.

Through research I came to the conclusion that I should use a Thread to listen to the synchronization.

The question is this: I saw 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. Syncing usually takes up to hours at the first sync. Should I really use the second option? If so, someone has a good tutorial that shows me how to do it because what I researched I can not apply to my problem.

Why can not AsyncTask be used for time-consuming tasks?

    
asked by anonymous 23.07.2018 / 19:49

1 answer

0

AsyncTasks, by default, work as a shared queue . All tasks are executed serially, one at a time, by a background thread.

This means that if you have a task that takes too long to finish, other tasks that are eventually queued will have to wait for them to be executed .

If you have the assurance that other tasks will not be queued when your synchronization happens, or that these queues will not cause problems, you can use them without any problems.

Otherwise, you can have AsyncTask work in parallel. Just run it with the executeOnExecutor(java.util.concurrent.Executor, Object[]) method, passing a Executor other than the default. AsyncTask already offers AsyncTask.THREAD_POOL_EXECUTOR , or you can create your own.

Note that this may bring bugs Parallelism due to indefinite execution sequence

As for an example of using Executor or ThreadPoolExecutor you can query AsyncTask's own source , since it also uses this mechanism to perform its tasks.

    
24.07.2018 / 19:03