问题
I have an application that uses a lot of AsyncTasks, the problem I have is that a particularly important task is not being started for upto a couple of minutes after I call execute.
If I use the following for my ICS devices it works;
if(Build.VERSION.SDK_INT >= 11)
{
myTask.executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, stuff );
}
as opposed to this on pre-ICS;
myTask.execute(stuff);
I am aware that ICS has changed thread execution to be serialised but I can't figure out what is holding up the thread queue.
There are about 20 threads listed in debug in eclipse, but I have read around that perhaps this is not correct as the debugger tends to keep displaying ones that aren't really there.
How do I figure out which threads are holding up the serialised queue so i don't have to switch from the default on ICS, and perhaps even improve performance of pre-ICS devices that arne't seeing the problems due to the thread pool executor being default behaviour.
回答1:
How do I figure out which threads are holding up the serialised queue
Add Log statements to track entry and exit from the relevant doInBackground() methods.
and perhaps even improve performance of pre-ICS devices that arne't seeing the problems due to the thread pool executor being default behaviour.
You don't need to use AsyncTask. Just fork your own thread and use stuff like runOnUiThread() as a means of executing logic back on the main application thread. AsyncTask is a convenience, not a requirement. And, for an outlier high-priority task, it may make more sense for you to simply keep that away from any thread pool contention.
Or, clone and fork AsyncTask to use a PriorityQueue, so you can explicitly indicate your high-priority tasks.
来源:https://stackoverflow.com/questions/11000680/android-asynctask-not-being-called-for-minutes-on-ics