Behind Android UI Thread

Things I never learned in two years of development | Day 7–#100DaysOfCode

Dhananjay Trivedi
3 min readSep 24, 2019

We know how a Thread works, it starts, executes some work and then terminates.

We know there is a UI Thread which is responsible for managing the UI but it doesn’t seem to terminate after it handles one of the operations, instead, it keeps on running and keeps on handling various UI operations.

The UI thread has a Message Queue which it has to execute. That message queue can be empty but still its something it has to execute the empty message queue and hence it never reaches the termination stage. If the message queue is empty, the UI thread is blocked but is waiting to respond to any changes in the message queue.

This message queue consists of pieces of work, one after the another. That is done using a Looper, a looper continuously loops through this message queue and sequentially dispatches the work to the UI thread for it to execute.

android.os.Handler

Handlers are the containers inside the message queue that takes up the work from other/background threads. You can use time/delay to put your work at a particular position inside the Message Queue. Looper checks for the timestamp and executes the work whose timestamp is now or in the past. Handlers have various advantages like Task Ordering, Thread Safety, and lower resource consumption by minimal context switching. Handler only works for a thread that has a looper and a message queue.

Handler has a post() method which takes a runnable as input, where we pass our task to be executed by the UI Thread as a runnable. The post() will actually convert it to a message and pass it as a message. The thread cannot directly add anything to the message queue, that is the handler’s job.

There is a Handler thread which you can use to schedule longer running tasks which you can’t use Async Tasks for, after your work is done you can pass it through runOnUIThread().

Documentation

android.os.Looper

Looper keeps the long-running thread containing the message queue.

This is a Java class inside Android user interface that works with the Handler class to process UI events such as view clicks, screen redraws and orientation switches. So you as a developer don’t have to worry about threading when you want to react to these events.

But when it comes to updating the UI, there we have a few things to keep in mind so that we don’t end up doing things on the main thread, freezing the UI. We create a Handler class that is associated with the main thread and then attach a runnable to it. The runnable is then later implemented via the Looper.

Some methods inside the Looper are:

prepare() — which is called to instantiate the message queue and attach it with the looper.

loop() — which runs the looper infinitely.

quitSafely() — Waits for any actively executed tasks

quit() — Abruptly quits all the task currently being executed.

Documentation

That’s all for Day7! Man its been an amazing week for me I hope the same for you!

--

--