Looper's loop method is called inside the ActivityThread's main method and starts an infinite for loop
which iterates over a message queue. When there is no more message in the queue then it calls nativePollOnce
method which waits for the next message.
So my questions are
1.if it waits for next message then the main thread will be blocked then how can it enqueue more message queue.
2.if it doesn't get blocked then it will consume the CPU cycle and others would not get a chance to add a message in the queue.
Please take into consideration that MessageQueue, Handler, Looper are on main thread and UI events and input events processed by the main thread.
Links
Why main thread's Looper.loop() doesn't block UI thread?
Why does Looper.loop() not block the UI thread
android - what is message queue native poll once in android?
Does an Android Looper thread use processing power?

- 219
- 1
- 3
- 15
-
According to [one of your links](https://stackoverflow.com/questions/8763594/does-an-android-looper-thread-use-processing-power), it blocks until the kernel signals that a message is available. It then wakes up and processes the message. – that other guy May 21 '20 at 21:15
-
that's mean the main thread is blocked until the kernel signals that a message is available. If It is blocked then how new messages will be pushed to queue(it is also on main thread). – Ankur Samarya May 22 '20 at 03:42
3 Answers
Messages don't need to be enqueued by the main thread itself. One of main use cases of a Handler
is to post messages to another thread. For example :
Create a
Handler
on the main thread, so that it is tied to the main thread's looper.Hand it to a another thread, which can post message to be handled by the main thread.
It is so common that every Activity
create it's own Handler
which is used in Activity.runOnUiThread()
to send UI task from background threads.

- 17,292
- 1
- 42
- 60
-
Yes, you are right we can enqueue the message from a background thread via Handler. But what android os do? Is it also enqueue all the messages from a background thread only - if yes it means that all the processing(event listening, callbacks) is catered by background thread. – Ankur Samarya May 26 '20 at 04:13
-
A thread can also enqueue messages on it's own message queue, to execute some task at a later time. Both cases (deferring a task and sending a task from a background task to the main thread) are heavily used by the OS – bwt May 26 '20 at 08:07
-
Let suppose the main thread is blocked since it is waiting for the next message then at that time how can it enqueue messages on its own message queue. It is a deadlock situation. – Ankur Samarya May 26 '20 at 08:17
-
In that case, it waits from a message from another thread. It can be a deadlock if the other thread(s) wait something from the main thread, but the typical case if for background threads to do something and send the result to main thread to update the UI – bwt May 26 '20 at 08:39
-
But if you design a system with your own `Looper`s and `HandlerThread`s using bidirectionnal messaging, a deadlock is definitely something that you must think of. – bwt May 26 '20 at 08:42
-
I could not understand your 2nd last comment completely. I am talking about if the main thread wants to push the message in its message queue and the main thread is waiting for the next message so it would create a deadlock. – Ankur Samarya May 27 '20 at 08:49
-
any thread can enqueue messages, including the thread running the looper. If this thread reaches the point where the queue is empty, it waits, for messages from other threads. In case of the main thread I'm pretty sure the queue is never empty. – bwt May 27 '20 at 10:52
-
By the way the main thread (or any HandlerThread) can also wait when the queue only contains messages scheduled at a later time. – bwt May 27 '20 at 10:54
If it waits for next message then the main thread will be blocked then how can it enqueue more message queue?
The main thread only runs the main loop. Everything else runs within the main loop itself. So if this loop is waiting for new messages, there's no other code running on the main thread at all. This also means that it's not possible that any new message is enqueued from the main thread itself.
If it doesn't get blocked then it will consume the CPU cycle and others would not get a chance to add a message in the queue. Please take into consideration that MessageQueue, Handler, Looper are on main thread and UI events and input events processed by the main thread.
The main thread is blocking until a message is received. This blocking mechanism is implemented on the native layer, thus it's optimized for performance.
The MessageQueue
and the Handler
are only data structures not directly connected to any thread.

- 38,113
- 8
- 108
- 143
This article describes this topic quite clearly, or you can see this answer.
In summary:
Looper is a very basic wrapper class which attach a MessageQueue to a Thread and manage this queue. MessageQueue is a structure to sequentialize simultaneous processing requests of a Thread. In Android, message/request processing classes like Handler uses Looper to manage their respective MessageQueue.

- 5,987
- 8
- 76
- 112

- 621
- 6
- 17