Photo by Matheus Bertelli from Pexels

Understanding of Thread, Handler, Looper, Message, Runnable and HandlerThread

Md. Asaduzzaman Noor
TeachMind
Published in
5 min readOct 16, 2018

--

Being an Android developer, you should know about these basic but important elements which will build up your basic knowledge. If you have any confusion to understand about any of the above elements then this post is for you. Or, if you want to use a library and just never interested about what is happening under the hood then you may stop reading here.

You may find the definitions easily, so let’s start from an example. I need to complete a task using Thread which will print some logs.

Now, when I said thread then you should be confused about which one I meant. We know the simple and mostly uttered line -> whenever an android app starts Linux OS run a process (only one per app) and that process will initiate a thread(as android framework is single threaded) to maintain the whole UI work named Main Thread or UI Thread.

With this single process you may start as many threads you want but only restriction is that you will not be able to run or accomplish any ui task. So, please remember that we have an issue here that..

we may want to complete any task by using a background thread and after finishing that task we need to update the ui.

We will know the solution later. Now we will see a simple thread example:

class test extends Thread{
@Override
public void run() {
Log.d("tttt", "test class thread is >"+Thread.currentThread().getName());
}
}

The log tag style is mine but the msg is very much important by which you will see and learn the core thing. The above code will be executed by the below statement:

new test().start();

Please run it now and try to understand from the output. Another element which I want to describe now is Runnable.

Runnable is nothing but an interface, please accept it. Okey, I will give you reference from Android Bible Link.

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

class testRunnable implements Runnable {

@Override
public void run() {
Log.d("tttt", "testRunnable class thread is >"+Thread.currentThread().getName());

}
}

Let’s sync -> should be implemented by any class(we used testRunnable) and whose instances (that means new testRunnable()) intended to be executed by a thread. So, new Thread() which is a thread but we have to put our runnable to be executed by this.

public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}

Don’t be afraid, the above is code is not mine. I just pasted it from Java Thread class. So, our statement will be:

new Thread(new testRunnable()).start();

Hey wait, why do we need this runnable? Now we are going in. This runnable is able to run by its own! Yes, it it.

Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d("tttt", "thread is >"+Thread.currentThread().getName());
}
};
runnable.run();

If you are 99% sure about the output then please run the above code because you should be 100%.

Now the question you have, what is the speciality when we run any runnable without a background thread, I mean in the main thread just like above. There is no meaning! Its just like a kingdom(Runnable) without the king (Thread).

Another simple question is what is the use case I mean why do we need runnable as we can execute anything within the Thread’s run method. Yes, we do need a runnable.

Suppose, you need to execute 2 different tasks in two different steps of a program using thread.

Runnable Task1 = new Runnable() {
@Override
public void run() {
//decode image
}
};
Runnable Task2 = new Runnable() {
@Override
public void run() {
//download image
}
};

Now you can pass these runnable as parameters to execute on any background thread at any time.

Handler is the most precious thing on android framework. Yes it is not a java thing. Before knowing about handler we have to know about two things Runnable and Message.

What is a Handler?

In simple words, Handler is not a thread and Handler is not related to the UI. Handler is just a way of communication, a medium, a bridge or such an element which is used to complete a big task(mostly any thread related task).

If I explain, communication between one thread to another. Medium of background thread to Main Thread or UI Thread. Now, we should learn about Message(as we know little bit about runnable).

By definition, a Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue.

I didn’t mention about MessageQueue because it’s the queue of Message. Message is something like a Bundle. Yes if you know about bundle when passing data from one activity/fragment to another, then just remember in case of Handler you can’t use bundle and you have to use Message Or you may use Runnable also. So, the point is that you are only able to use a Message object or a Runnable object to make any handler functional.

new Handler().post(new Runnable() {
@Override
public void run() {
Log.d("tttt", "thread name >"+Thread.currentThread().getName());
}
});

If you just type new Handler().p then android studio will show you the suggestions where you can easily understand that all the other methods asking for runnable as the mandatory parameter. That means if you want to run any task after a certain amount of time or at any specific time then you have to use the combination of Runnable and Handler(you can’t make using one).

As I said above that Runnable is just like Kingdom and here Handler is the King. So, Runnable can’t do anything by its own, means it needs to go with a Thread or a Handler.

To describe about the relation and use of Message and Handler we need to know about Looper. I will discuss the remaining elements to the next posts.

Please clap and share for more useful stories. Thanks a lot.

--

--

Md. Asaduzzaman Noor
TeachMind

Android App Developer using Kotlin, Java and Android Studio