Android Fundamentals: Magic of threads – Handlers and Message Queues

Second article of the new series about Android Fundamentals in which I’d like to touch on some grounds of Android operating system.

First article – Android Fundamentals: Magic of threads – Loopers

1. Big picture

Imagine that user touches the screen of his phone few times in a row. All these events must be handled so we need some kind of a data structure to store these instructions. As it turns out, we have a queue inside every Looper. But how these events get enqueued?

 2. Communication between threads

Intuition: Handler is a bridge between threads

These actions are enqueued thanks to the Handlers which are responsible for handling messages. The whole process is as follows:

  1. We send a message from a background thread to the Main Thread

2. Handler receives a message from a background thread and passes our message to the Message Queue which is located in the Looper

3. Once previous message has been finally processed then our message is sent back to the Handler

4. Then message is sent to the Activity on the Main Thread (actually CustomHandler also works on the Main Thread)

Finally, our message is delivered!

m5chnk24xrzlw

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s