Although I don't have a reference at hand, I think it's safe to assume that Android executes AsyncTask.doInBackground() in a Thread with priority android.os.Process.THREAD_PRIORITY_BACKGROUND
This means that this Thread is scheduled in the context of a Linux cgroup (scheduling class) for which -- always or under frequent circumstances, I'm not sure and have read various claims -- an upper bound of common CPU time of 5% or 10% -- again, different sources make different claims -- is applied.
In other words, all background threads have to share 5% or 10% of the available CPU time. Again, I have read claims that this is dynamically adjusted if the foreground and real time tasks are idle, but I'd be happy to be pointed to a credible source myself. Also, I wouldn't count on it since the user can listen to a real time audio stream while using my app.
If you adjust the background Thread's priority, like so:
private static final int bgThreadPrio = Process.THREAD_PRIORITY_BACKGROUND +
Process.THREAD_PRIORITY_MORE_FAVORABLE;
protected YourReturnType doInBackground() {
Process.setThreadPriority(bgThreadPrio);
....
}
then you achieve two things.
- You lift the Thread out of the background cgroup such that it does not have to share 10% of CPU time with the other background threads (at least currently, until Android changes its policies in this regard).
- You assign a priority to the Thread which usually will not have an extremely bad impact on User Interface and Real Time threads, because THREAD_PRIORITY_DEFAULT is 0 while THREAD_PRIORITY_BACKGROUND is 10. So your Thread will run at priority 9 which is much worse than 0 but it will avoid the artificial limit of the background tasks.
However, you also probably change the priority of the Thread which the underlying AsyncTask executor provides for your AsyncTask. This Thread is going to be recycled, and it may be a single Thread or chosen from a pool. So it might be a good idea to set the priority in all doInBackground() methods in all AsyncTasks in your app.