[Android] Main Thread — You See but You don’t Observe

harshit.droid
4 min readMar 26, 2020

--

If you had spend some time with Android Studio then you might have heard about Main Thread, we all know that when User launches an App, Android Framework creates a Process and a Main Thread (UI Thread) for core tasks like Inputs, Layout Inflation and UI Drawing etc. This main thread handles events from all over the App.

Example: App Callback, Input Event Callback, Service etc. These Callback can trigger other work which runs on the Main thread like UI changes, Calculation etc.

Note : Ordinarily, an app’s Main Thread is also the UI Thread, but under special circumstances, an app’s Main Thread might not be its UI Thread.

You can learn more about this in Thread annotation

So, we can say that a Thread is a single sequential flow of control within a program. In simple words thread has a beginning, a sequence and an end.

If you give it a thought you might come to a conclusion that in android this Main Thread is also a Thread and it should also die after executing particular sequence.

Then problem arises if this main thread dies then who will perform the core tasks like inputs and UI drawing etc. We need something which can restart the sequence repeatedly whenever user interact to our app

To solve this problem Android Framework already has Looper Class. As the name suggests Looper Class enables thread to keep on running in a loop.

Let’s Understand How Android Framework Handle The Threads

The Main thread consists of a Message Queue which needs to be executed. The execution never reaches the termination stage, since that message queue can be empty but still it has an existence and therefore needs to be executed. Due to this, 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, which is done simultaneously using a Looper Class, a Looper continuously loops through this message queue and sequentially dispatches the work to the UI thread for it to execute.

Documentation.

Now, we know how Main Threads are handled in Android Framework, it runs continuously until activity gets destroyed and is responsible for performing core tasks.

So, we can conclude that any block of code we want to run will directly run on Main thread. When app performs intensive work based on user Interaction then this single thread can give you poor performance. If Main Thread is blocked by some chunk of codes then App hangs. Blocking UI Thread for more than 5 seconds will launch ANR (Application Not Responding) Dialog.

Picture Courtesy

So, Ideally those work which are more resource intensive and takes longer time to execute like network calls, downloading a large file, uploading a large file etc. should be shifted to other thread, in android we call these threads as Worker Thread.

There are few methods you can use to implement the Worker Thread

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long

Note : Android UI toolkit is not thread-safe. So, Avoid UI Components from worker threads.

You can also Use

HandlerThread- threads for Callback, you can create handler for the particular thread.

AsyncTask- do work in background and send result back on UI Thread.

ThreadPoolExecutor break lot of works into small tasks. and do it in parallel.

IntentService- it can keep task out of UI Thread but not ideally.

learn more here.

--

--

No responses yet

Write a response