while it is very convenient to use, from my understanding, AsyncTask has two important limitations:
doInBackgroundof any instances will share the same worker thread, i.e. one long runningAsyncTaskscan block all others.execute,onPostExecuteand other "synchronizing" methods must/will always be executed on the UI-thread, i.e. not on the Thread, which wants to start the task.
I ran into trouble, when I tried to reuse some existing AsyncTasks in a background IntentService that are responsible for the client-server communication of my app. The tasks of the service would fight over time in the worker thread with those of the UI activities. Also they would force the service to fall back onto the UI-thread, although that service should perform its work quietly in the background.
How would I go about removing/circumventing these limitations? I basically want to achieve:
A framework that closely resembles AsyncTask (because I need to migrate a lot of critical code there).
Each instance of such a task should run its
doInBackgroundon its own thread instead of a single worker thread for all instances.Edit: Thx to VinceFR for pointing out this can be achieved by simply calling
executeOnExecutorinstead ofexecute.The callbacks like
onPostExecuteshould be called on the same thread that started the task by callingexecute, which should not need to be the UI-thread.
I figure, I'm not the first person to require something like this. Therefore I wonder: Is there already some third-party library that can be recommended to accomplish this? If not, what would be a way to implement this?
Thanks in advance!