handler thread in android

Using Android Handler Thread in Android for Multi-threading

Do you want to learn about Android Handler Thread for multi threading ? We have multiple ways to accomplish the problem at hand.

But here we will specifically use the Android Handler Thread to accomplish our multi threading task.

I had previously written about async task’s deprecation and what you should do about it. Here we will look at the multi threading.

What is Handler Thread in Android ?

The official android documentation states the HandlerThread as

Thread that has a Looper. The Looper can then be used to create Handlers. Note that just like with a regular ThreadThread.start() must still be called.

Now let us look at unofficial definition of HandlerThread class. It is just another subclass of Thread which will help you create a new non UI thread.

How to use Handler Thread in Android ?

Before actually implementing the handler thread let us look at how we can do it in Android.

Here are a list of steps we will be doing

Step 1: Create a new instance of handler thread by passing in the name for the thread.

Step 2: Invoke the .start() method in the instance of handler thread.

Step 3: Get the Looper from handler thread and create a new Handler instance.

Step 4: Create a new Runnable instance for passing in to the Handler.

Step 5: Pass in the Runnable instance.

Handler Thread Implementation Using Java

Now let us look at the source code for the HandlerThread. The code is self explanatory, and I have also included the comments in the code.

// create and start new Handler Thread HandlerThread handlerThread = new HandlerThread("MyBackgroundTask"); handlerThread.start(); // create new instance of Handler passing in the Looper from handlerThread above Handler handler = new Handler(handlerThread.getLooper()); Runnable runnable = new Runnable() { @Override public void run() { // put your code that must run outside } }; // here we have to execute the runnable code handler.post(runnable);
Code language: Java (java)

Now let us convert the same code to Kotlin implementation.

Handler Thread Implementation Using Kotlin

Let us look at the same code in Kotlin. If you are just starting out with Kotlin.

// create and start new Handler Thread val handlerThread = HandlerThread("MyBackgroundTask") handlerThread.start() // create new instance of Handler passing in the Looper from handlerThread above val handler = Handler(handlerThread.looper) val runnable = Runnable { // put your code that must run outside } // here we have to execute the runnable code handler.post(runnable)
Code language: Kotlin (kotlin)

Now that we have seen the implementation for handler thread let us learn something more about it.

When should you use Handler Thread

As we all know the AsyncTask has deprecated and there are multiple ways of multi threading. You should use this if you want to just do certain task once or twice.

Meaning, your application doesn’t really depend much on multi-threading and it’s just dead simple. You are okay with using these kind of sub classes of Thread.

If you heavily depend on multi-threading. I recommend using co-routines introduced in Kotlin’s API. But if you are still using Java or managing legacy code.

Posts You Should Read

Show or Hide Soft Keyboard in Android Application And More

OnClickListener in Android RecyclerView: Example

Using Bottom Navigation Bar in Android: An Easy Guide

Android Notification Manager: Create Notification in Android

Learn Android Room Persistence Library for SQLite Database

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *