I realize you get more customization but are there performance (mem/speed) benefits to using the concurrent library classes over just using the stock AsyncTask or just creating threads and calling run?
I realize you get more customization but are there performance (mem/speed) benefits to using
Share
Mostly it’s ease of use that AsyncTask brings. That paradigm of working, run this method in the background, and this other method on the UI thread is great for usability (although I’ve made improvements that make exception handling simpler by creating a subclass of vanilla AsyncTask). I think AsyncTask is great for calling services with a request-response type calls. But, not all types of concurrency are valid for AsyncTask.
Now, there are some tasks that AsyncTask aren’t suited for. For example, if you want to load multiple images from a server at once. AsyncTask might not be the best option. For one AsyncTasks can only be run once and can’t be restarted. AsyncTasks own the thread they run the background method on and that thread can’t be reused or shared. So if you had 10 or 20 images that means 10 or 20 threads to create and shutdown if you used AsyncTasks. Or load all images at once which means your UI won’t update until all images are fully loaded from the server through 20 separate calls. Either you pay for more memory, resources, etc for nice UI response, or your UI pays the price for lower overhead. Neither is great.
A better option is to load a thread once, send it several light weight jobs to run, and post those back to the UI thread. I created my own version of something like AsyncTask that I call PipelineHandler which creates a single thread, and runs jobs posted to it. Those jobs are something very similar to AsyncTask in that they have a method that is called off the UI thread, and another method that is called on the UI thread to perform updates. The benefit here is that I can leverage a single thread to run as many jobs as I wish. And I can easily scale up the threads servicing the queue as needed without needing to changing anything but a configuration option. Under the covers it uses the Java concurrent classes ExecutorService, but also leverages Handler to provide an ability to run something back on the UI thread. So it combines Java Concurrent lib and Android lib together to build this.
So to answer your question I think AsyncTask is great for single time jobs like service request-response type calls, but not for batch processing like downloading images or downloading files, especially if you have to make many calls at once. Java concurrent libraries can play a part in helping you perform those types of tasks, but are limited because they only solve half the problem, namely, running things on threads. Java concurrent libraries ARE good for batch job running, but aren’t good with interfacing with the UI thread. So you will have to combine them together, more often than not, to get a solution.