Threading With HandlerThread in Android

You will learn how to use HandlerThread to receive messages from a Runnable in an Activity and pass them back to a UI handler to update the UI. By Amanjeet Singh.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Cleaning Up

To clean up, you will add some code in onDestroy() of MainActivity. This code will ensure that the orderHandlerThread and the foodRunnable stop properly when the Activity is destroyed. Override the onDestroy() method of MainActivity with the following code:

override fun onDestroy() {
  super.onDestroy()
  foodRunnable.stop()
  orderHandlerThread.quit()
}

The above code stops the foodRunnable and the orderHandlerThread by calling stop() and quit() respectively on their instances.

And that's all you need to assemble for your HandlerThread!

Build and run your app. You will see the following:

Summary of Interaction Between Threads

Here's a quick recap of what happens throughout the entire app. The MainActivity creates 10 orders through the FoodRunnable. The OrderHandlerThread receives the orders, converts their prices from USD to INR, and attaches the additional side orders to them. Finally, OrderHandlerThread forwards the processed orders back to the UI through the UiHandler, which is bound to the UI of the app. This UiHandler updates the item list in the FoodListAdapter and then notifies the RecyclerView adapter.

Use Cases of HandlerThread

Consider some use cases of HandlerThread, which you can use in your day-to-day Android development:

  • They are useful when you need to implement a Producer/Consumer paradigm between different threads.
  • It simplifies the creation of the Looper instance that is bound to a specific thread.
  • It is used in the Android Service implementation in order to execute some tasks off of the main thread.

Comparison With Other Options

Now, compare HandlerThread with other related options for doing concurrent programming in Android:

  • AsyncTask: This is a Facade that simplifies the usage of the HaMeR framework components when you have to run simple tasks in the background and produce a result to be displayed in the UI.
  • Service: Activities are not a good place for threads because they can be killed by the system if it needs resources. On the other hand, Services are components that Android preserves with higher priority and are a better place for a thread to run. Because Service run on the main thread, a HandlerThread is often a good solution.
  • IntentService: This is a Service implementation that creates a HandlerThread in order to run tasks sequentially. This is useful if you need to run tasks using the fire-and-forget paradigm.

Note: It's important to recognize here that IntentService uses HandlerThread internally. You can look at the source code here.

Note: It's important to recognize here that IntentService uses HandlerThread internally. You can look at the source code here.

Where to Go From Here?

Good Job! Now, it's time to look back to what you've learned in this journey:

  • What HandlerThread is and its role in the HaMeR architecture.
  • How to create your own HandlerThread implementation.
  • How to manage UI changes from a background thread.
  • Use cases of HandlerThread.
  • Differences between HandlerThread and other classes like AsyncTask, Service and IntentService.

You can download the final project by using the Download materials button at the top or bottom of this tutorial.

If you want to learn more about HandlerThread, look into the Android Documentation.

You can also learn more about concurrency on Android by checking out our Android Background Processing video course.

I hope you enjoyed this tutorial on HandlerThread. Join us in the forums to discuss this tutorial and your findings as you work with HandlerThread!

Amanjeet Singh

Contributors

Amanjeet Singh

Author

Massimo Carli

Tech Editor

Sean Stewart

Illustrator

Meng Taing

Final Pass Editor

Eric Soto

Team Lead

Over 300 content creators. Join our team.