Internals of HandlerThread in Android

Deep dive on understanding how HandlerThread class is implemented

Internals of HandlerThread in Android

In previous parts, we've delved into Handler, Looper and Message, understanding their roles in enabling code execution within a specified thread and facilitating safe communication between threads.

The HandlerThread class combines these concepts, simplifying the acquisition of Looper and Handler objects. In this article, we'll explore the implementation of HandlerThread referencing some AOSP code examples.

Internals of HandlerThread

HandlerThread extends the Thread class, providing additional functionality for initializing a Looper and Handler for the associated thread.

Here's the class definition:

 * A {@link Thread} that has a {@link Looper}.
 * The {@link Looper} can then be used to create {@link Handler}s.
 * <p>
 * Note that just like with a regular {@link Thread}, {@link #start()} must still be called.
public class HandlerThread extends Thread {
    // SajalRG: Implementation details...

UML class diagram of HandlerThread is shown below: class diagram handler

As demonstrated, HandlerThread essentially functions as a regular Java thread with added capabilities for constructing Looper and providing access to the Looper and Handler instances.

Recap: Construction of Looper

Examining the run() method of HandlerThread, we observe the process of Looper initialization, as discussed in first article of this series.

Looper.prepare() will create a new instance and can be called only once inside a Thread. Looper.loop() waits for message to appear in MessageQueue and process them. For detailed information and innerworking of this, I suggest to read this article.

    public void run() {
        mTid = Process.myTid();
        synchronized (this) {
            mLooper = Looper.myLooper();
        mTid = -1;

Uses of HandlerThread

Now that we understand what HandlerThread is and how it operates, let's explore its practical utility.

Example1: Setting's App -

In this example, HandlerThread is used to obtain a Handler object for the underlying Thread. View Full Code Here.

        if (mIsWifiTrackerLib) {
            // SajalRG: construct new HandlerThread
            mWorkerThread = new HandlerThread(
                    TAG + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
            mWorkerThread.start(); /* SajalRG: actually starts the thread */
            // SajalRG: other code removed for simplicity
            mNetworkDetailsTracker = FeatureFactory.getFactory(this)
                            new Handler(Looper.getMainLooper()), /* SajalRG: passing handler for main looper */
                            mWorkerThread.getThreadHandler(), /* SajalRG: passing handler for the new thread */

Example 2: Camera2 App -

Here, HandlerThread is utilized to acquire a Looper for creating a new Handler object.View Full Code Here.

    public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
        // SajalRG: other code removed for simplicity
        HandlerThread thread = new HandlerThread("CaptureModule.mCameraHandler");
        thread.start(); /* Thread started */
        /* Create new Handler using looper from handler thread */
        mCameraHandler = new Handler(thread.getLooper());
        // SajalRG: other code removed for simplicity


HandlerThread simplifies the creation of a Looper, which otherwise would require manually extending Thread class and overloading the run() method with similar implementation. Understanding the functionality of HandlerThread equips us to leverage its benefits in our projects effectively.

In the next article, we'll address potential memory leak issues with Handler and how to mitigate them.

If you find these articles helpful, please consider liking or commenting to support future content. If you have any questions or topics you'd like covered, feel free to leave a comment below.