AsyncTask: Beware of Curves Ahead

Somebody tweeted yesterday a link to this code change to AsyncTask This suggests that the core Android team made good on a veiled threat of changing the AsyncTask behavior to only execute one task at a time.

That being said, in some tests this morning, I cannot seem to get the lightly-documented behavior. If I fire up several AsyncTask instances, they still all seem to run in parallel on 4.0.3 in the emulator and on a Nexus S. Perhaps somebody on the core android team has a different definition of “serialized” than I do – I was expecting behavior more like IntentService, where each command sent to it will be processed one at a time, not in parallel.

It is very possible that parts of this patch did not make it into the product as yet. If nothing else, the JavaDocs for execute() do not match the JavaDoc comment on execute() as shown in the patch. And, there was no note of an AsyncTask behavior change in the API Level 14 or 15 SDK release notes, and I would hope that a change of this magnitude would not be swept under the rug.

That being said, I would keep a very close eye on AsyncTask for the foreseeable future. If you are definitely relying on AsyncTask using multiple threads in parallel, you should consider starting to use executeOnExecutor() instead of execute(), perhaps using the stock THREAD_POOL_EXECUTOR, or perhaps using your own Executor.

And, in the meantime, I get to figure out why the R17 tools seriously screwed up Eclipse’s ability to build projects…