Using HandlerThread in Android

Frank Tan
3 min readApr 3, 2016

--

Android offers high level multi-threading options, such as AsyncTask and IntentService to offload long running blocking tasks to a seperate thread. HandlerThread is a low level threading alternative which is not discussed as often. HandlerThread allows you to run a worker thread with a single message queue. It becomes very handy when

  1. you want a really light weight alternative to run some very simple tasks, where AsyncTask and IntentService become an overkill, such as communicate with camera or accelerometer.
  2. you want to do some short and simple tasks during the life cycle of an activity or fragment, such as file or database operation. It is worth noting that AsyncQueryHandler class provided in Android framework uses HandlerThread to make database CRUD operations asynchronous.
  3. you need some simple tasks to be processed sequentially on a worker thread.

To use HandlerThread, it is important to understand how Android manages message passing in between threads.

Message Passing in Android

Handler is the message processor on the worker thread. Message is data or work (runnable) offloaded from, for example, UI thread. When a message is received on Handler, it appends the message to the Message Queue. Each thread can have only one Message Queue. Looper is a message dispatcher which takes one message at a time from the Message Queue and dispatch it to the Handler for processing.

Knowing how the message passing works, HandlerThread becomes very simple. Here I extended HandlerThread class to allow custom handling logic (sleep and post message to UI thread). Of course, in real life projects, you would probably do file operations or accessing hardware, etc.

And here is the code for the UI thread in the activity class. It creates and starts the CustomhandlerThread worker thread in onCreate and clear its message queue and stop the running task in onDestroy. When the buttons are clicked, they send a message to the worker thread. The activity also receive messages from the worker thread through a handler on the UI thread.

Avoid Memory Leak

One thing you should watch out for when using HandlerThread or in fact any multithreading technique is memory leak.

  1. Avoid using non-static inner classes in activity. Non-static inner class will have an implicit reference to the hosting activity and will stop the activity from being destroyed.
  2. If you really need to have a reference to the activity, use a WeakReference.

Avoid Mixing Long Running Tasks with Short Ones

Because HandlerThread has only one message queue and all messages are processed sequentially, long running blocking tasks will make all short running tasks waiting. A better way is to seperate long running tasks to a seperate HandlerThread or use other threading techniques.

Source Code

The source code for the example used in this article can be found in this Github repository. You can also check the implementation of AsyncQueryHandler class from Android framework here.

--

--

Frank Tan

Solutions Architect, Software Engineer. Helping companies to achieve their desired business outcome. Opinions are my own.