Android Programming Programming

Patterns for running Asynchronous code in Android (Part 1)

An important technique in Android Programming is to make potentially long task run asynchronously in your app, avoid blocking the UI thread, so your app feels more responsive. For example, downloading a file from the Internet, loading a media file from the external storage or applying a filter on an image bitmap etc. I will show you how to implement with AsyncTask, simple java threads and java Executor in Part 1.

Let’s say I want to load a bitmap from the internet, and set it to an ImageView , this might be what you’d write at first:

The Java way: simple Threads

You can implement it just like any other Java programs, define a Thread that does the job, and run it. Every Java programmer knows how to do that. Remember that if you want to modify the UI, you need to use runOnUiThread()


The Java way: Executors

Executors are like managed threads, you can bound the number of running threads. In many ways, it behave just like Threads. You can define a thread pool of size 1, then the Runnables will be executed sequentially.

The Android way : AsyncTask

AsyncTask is a built in utility in the Android framework, the basic usage is to extend the class and implement your own code in the doInBackground() method, override also the onPostExecute() if you need to run some UI updates base on the returned results.

In your Activity:

 Which one should I use?

The 3 methods can all do the job, however there are important differences that you need to know. Java threads are cheap, easy to implement and use. The problem is that if you don’t write extra code the manage the threads, you may easily create dozens of threads, overwhelms the system and the server. Java Executors are a better choice, because it is essentially a thread pool, you can limit the number of running threads at any time.

AsyncTask comes with Android, so it looks like it is the obvious choice. It integrates nicely in your Android code, but there’s a small detail you might have missed, it does not promise you concurrency. In my experience there can only be only 1 AsyncTask running in your app at any given time. In other words, if you fire up multiple AsyncTasks, they will be executed sequentially. That means if you are writing a multi-threaded function, don’t use AsyncTask.

There’s actually more patterns to run code asynchronously, In next part I will talk about Loaders, Service etc. (Still trying to understand better before writing about it) Stay tuned!

2 replies on “Patterns for running Asynchronous code in Android (Part 1)”

“In my experience there can only be only 1 AsyncTask running in your app at any given time”

You can have as many running in parallel as you want. On older devices, that is the default behavior. On newer devices, you typically have to opt into that behavior, using executeOnExecutor() rather than execute().

Cool! thanks for the information. Android is evolving very fast, I haven’t updated this article since I wrote it, looks like some of them is no longer true. Thanks for remainding

Leave a Reply to commonsguy Cancel reply