Android- Handlers, Loopers, MessageQueue Basics

Anmol Sehgal
6 min readMay 23, 2020

--

Probably the most important APIs of Android suited for Multithreading and offloading the tasks to worker threads- Handlers and Loopers.

The Android architecture has the Main Thread AKA UI thread, which updates the UI after every 16ms frame. Failure to update within this window will reflect as the “lag”, and even worse if it fails for 5secs, then the “Application not responding” shown to the user- marking the app crash. To avoid these types of issues, please resort to offloading the heavy tasks to worker threads.

To explain: the Android architecture has one main/UI thread which should ideally only be responsible for updating the UI, and other tasks should be done on separate threads(called worker threads). These worker threads upon completion should send the result back to the main thread and the main thread can then use it to update the UI, etc.
There are some Android APIs to do so like Async tasks, Services(Intent services actually), Executors, etc, etc.
But there are even cooler APIs available and are the core of all the above-mentioned APIs- Handlers and Loopers. Even Async and Intent Services rely heavily on them. Let’s see how they work.

Handlers and Loopers Architecture:

For the thread to receive any message from other threads, the most common pattern we follow is that it should read from the common data structure(usually Queues), to which other threads can write. The same is being followed here.

Handler Threads:

These are the threads that can receive messages from any other thread. Main/UI thread is also a Handler thread.
As explained above, such a thread must need a Message Queue(data structure in which other threads can write to), and a mechanism to read this queue infinitely(until stopped).
Looper is a mechanism which loops through the message Queue infinitely until stopped.

Message Queue

Each HandlerThread has one Message Queue into which other threads write data to(via Handlers), and it has a Looper which keeps on reading from it.

Looper:

A Looper is associated with every handler Thread and a Message Queue.
It keeps on reading the MessageQueue for any new Messages, and once it reads a message it will delegate that to the corresponding Callback(Handler).

Handler:

The handler is the exposed API to work with Loopers/MessageQueues. It basically exposes methods to interact with the Looper architecture, like writing messages to MQ, Setting callbacks to handle the message once Looper reads it, etc, etc.
So Each handler will need a Looper(so that it knows in which MQ to write data to since Looper has the reference to the MQ).

Message:

The Messages which can be sent to the HandlerThread. To avoid the number of Messages initializes, there is a pool maintained. Every time the looper is stopped, or the message is read by Looper, it is recycled(i.e. its fields are cleared off), and added to the pool. Next time we want to create a new Message, we get it from the pool(Message.obtain() method does that). This way the Android prevents us to initialize new Message instances every time.

The message is the LinkedList:

Line 1: Its next points to the next Message.
Line 2: It has long when signifying the timeInMillis of when should message be processed/read.

Please note that the LinkedList of these messages should be processed by their when, so it's sorted on when.
When adding a new msg, we iterate the LinkedList to find the particular slot to insert this message and then insert it there.

Sending Data to UI thread(which is the Handler Thread too):
Say a worker thread(created at line 1) wants to send data(123) to UI thread:

Line 1: Creates a new worker thread.
Line 4: Get the Looper associated with the UI thread
Line 5:Create the Callback which will be called by UI thread, once it reads the message we are going to send to it.
Line 12: Create Handler with the Main Thread’s Looper, so that we write the message to Main thread’s MQ.
Line 13: Create an empty Message from the pool of messages( to avoid GC overload).
Line 14: Put some value into the Empty message — 123.
Line 15: Setting the above handler(which has callback defined) as the Message Target, which will be called once the message is read by the UI looper.
Line 16: Write the Message to the MQ via Main Looper via Handler.

This is pretty easy to write to Main thread from worker thread, as the Main thread is itself a handler thread which has its Looper/MQ etc defined.
But what if we want to write our own Handler Thread to which other threads can communicate?
In that case we need to initialize Loopers, MQ, etc ourselves.

So make a Thread a Handler thread, we need to do:
1. Looper.prepare() → basically initializes the Looper and creates an MQ for it.

2. Looper.loop() → This will start the looper, which will keep on reading the Message Queue until Stopped.

Line 2: This will get the MQ for that Looper.
Line 8: Read from MQ infinitely until stopped(in that case msg read from Queue will be null).
Line 9: gets blocked until the message has arrived in the MQ. queue.next will be blocked until it sends a msg, it will send null only if Looper is stopped(using Looper.quit() method).
Line 15: Once msg is read, send it to its callback(defined in msg.target and hence in Handler’s callback).
Line 17: Once msg is handled, recycle it to clear its data off, and add it to the pool.

These above 2 methods are sufficient to create a Handler Thread.

Another easy way is to extend the Thread by HandlerThread, which under the hood calls these 2 above methods.

This is the HandlerThread run method: which calls Looper.prepare — then Looper.loop and in between onLooperPrepared to do something in this thread.

How to send data to such a Handler thread?

Simple, instead of passing the MaineLooper to the Handler, pass the Worker Handler Thread’s Looper like:

Line 6 is the only difference, here the looper belongs to the Handler thread we created and not that of the default UI thread.

Let’s explore the working of other Methods too:

MessageQueue::next

Looper.loop depends heavily on Queue.next() method. Below is the stripped-down version of next:

MessageQueue has the LinkedList of Messages(sorted on Message.when). Message.when corresponds to the time this message should be processed.

Line 6: Get the LinkedList of messages written into the Queue.
Line 7 and 8: If the message exists and message read has when before the current time(I.e. it is eligible to be processed), read it.
Line 10 and 11: LinkedList points to the next message now, as this message is read and could be removed from the Linked list(its next is set to null).
line 12: set this message in use, so that no further action can be done on it, like updating it, etc.
line 13: return it to the Looper. This will not happen until any message is read, as there is an infinite loop enclosing this code at line 2.
Line 17 and 18:
Returns null only if it is stopped.

Adding Messages to Queue:

This is done via handler.sendMessage() or Handler.post()

2 types of messages can be sent to HandlerThread:
a. Messages: which have when, data, target, etc fields. Upon being read by the Looper, it delegates it to msg.target to handle the message. This will run on the HandlerThread, and not on the worker thread which sends this message.
b. Runnable: Upon read by the Looper, the Runnable.run() is called inside the Handler Thread. Under the hood this Runnable is converted into the empty message, and this runnable is set as its callback. So that this runnable also acts as a Message.

Handler.post(Runnable )

Line 2 wraps this Runnable into the Message, and its the Message which gets written into the MQ.

Handler.sendMessage(Message )

As shown, this basically calls queue.enqueueMessage to write this message into the mQueue(MessageQueue for this handler thread).

MessageQueue.enqueueMessage(Message )

Line 2: If it’s already being read(next() sets this boolean true), or already added into Queue(line 13), then don’t add the same message again.
Line 7–10: If the Queue/Looper is quitting, then don’t add msgs into it.
Line 13: Set this msg to be in use so that it’s not added into the queue again.
Line 16: If this msg is to be processed before or at the current time, add it at the front.
Line 17–18: LinkedList code to add msg at the head.
Line 21–27: Find the slot to add this msg based on its when, as the LL is sorted on its when. This is a general LinkedList iteration code.

Please note that all the above methods are stripped-down to ease the understanding.

So the basic design follows:
1. Handler thread can receive messages from other threads.
2. Handler thread has a looper that reads queue for any new messages.
3. Handler Thread has a Message Queue where the messages are written by other threads via a handler.
4. Looper loops until it reads the message, and then it calls the callback function set by the worker thread, within the Handler Thread.
5. And thread can write to this HandlerThread if it has its looper.
He can do so by creating a handler(with its looper as param), and then sending messages via this handler.
6. Looper.getMainLooper() returns the looper for mainThread, which can be passed into the handler to write to the MainThread directly from any other thread.

--

--