while it is very convenient to use, from my understanding, AsyncTask
has two important limitations:
doInBackground
of any instances will share the same worker thread, i.e. one long runningAsyncTasks
can block all others.execute
,onPostExecute
and 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
doInBackground
on 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
executeOnExecutor
instead ofexecute
.The callbacks like
onPostExecute
should 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!