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.

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        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 - WifiDialogActivity.java

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)) + "}",
                    Process.THREAD_PRIORITY_BACKGROUND);
            mWorkerThread.start(); /* SajalRG: actually starts the thread */
            // SajalRG: other code removed for simplicity
            mNetworkDetailsTracker = FeatureFactory.getFactory(this)
                    .getWifiTrackerLibProvider()
                    .createNetworkDetailsTracker(
                            getLifecycle(),
                            this,
                            new Handler(Looper.getMainLooper()), /* SajalRG: passing handler for main looper */
                            mWorkerThread.getThreadHandler(), /* SajalRG: passing handler for the new thread */
                            elapsedRealtimeClock,
                            MAX_SCAN_AGE_MILLIS,
                            SCAN_INTERVAL_MILLIS,
                            mIntent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY));
        }

Example 2: Camera2 App - CaptureModule.java

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

    @Override
    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
}

Conclusion

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.

0
Subscribe to my newsletter

Read articles from Sajal Raj Gautam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sajal Raj Gautam
Sajal Raj Gautam

I’m a software engineer with a focus on Android applications and framework development. I specialize in tackling complex challenges with innovative solutions. I’ve worked across various form factors and software layers, contributing to the various software development lifecycle—design, development, testing, planning, and performance optimization. Leading teams, I ensure quality deliverables within deadlines. Beyond Android, I’ve gained hands-on experience in Robotics, IoT devices, RTOS applications, and Machine Learning. My dynamic career journey reflects my enthusiasm for learning and adapting to new technologies. I’m eager to explore opportunities and contribute to cutting-edge developments in the tech industry.