2

Is the Android Handler an implementation of concurrent message passing?

It seems that by using HandlerThread, it's easy to communicate between several worker threads. But, as I understand it, message passing in concurrency is based on using shared memory. I tried to understand the mechanism of implementation of Handler, but it's still not clear for me.

How does the Android Handler work in simple words?

2
  • Have a look at my answer I gave on Mutli-threading stackoverflow.com/questions/11596708/… Commented Nov 2, 2012 at 9:35
  • subject is still unclear or nobody is interested in it? Commented Nov 2, 2012 at 13:02

3 Answers 3

3

Yes, a Handler provides a form of inter-thread message passing. The Handler object itself is the "shared memory" that is accessible from several threads, but it's thread-safe and manages the details for you.

More specifically, a Handler is a convenient tool for interacting with the message queue (MessageQueue) for a given thread. When you post a Runnable or Message to a Handler, they're added to the MessageQueue for the Handler's thread. The thread, for its part, runs an event loop (Looper) that continuously processes messages in the thread's queue.

If you want to understand the details of the Android implementation, the code for Handler.java (in Android 4.1.1) can be viewed on GrepCode. The documentation for that class is quite detailed, and the code is approachable.

Sign up to request clarification or add additional context in comments.

Comments

1

Short answer: yes

Long answer: @acj answer is a good short and concise explanation of the specifics of Handler / Looper / MessageQueue. However, it does miss couple of points.

Each thread can have associated message queue, where messages sent to it are stored until the thread gets to execute them. The messages are processed sequentially in a loop running on the thread. Other threads can post messages into the queue; however, the message processing is asynchronous, and the other threads are typically not blocking on it.

There are number of standardized messages (like the Runnable message) which are executed by the OS implementation of the loop. However, the application code might support processing custom messages as well.

Handler is just a handy way for posting messages to the message queue for particular thread (typically the thread the Handler object is created on) from other threads. Note that Handler can be used in-process only, and in a single process all memory is common across all threads, so the message queue does not need to be kept in "shared memory" (that term is used for memory regions accessible by multiple processes usually).

One specific thing to keep in mind with all this is that posting a message using a Handler does not guarantee it's processing. It's possible for the thread to exit for any number of reasons before it gets to your message.

Comments

0

As you know, you can only make changes to UI in UI-thread. So when you need to do something in background and display result in UI but for some reason you don't want to use AsyncTasks (for example, you need to run several tasks at one time) you could use Handler. It could receive messages from not-UI threads and due to these messages make changes to UI (in UI-thread of course).

For example we implement handler in OnCreate() method:

TextView tvInfo = (TextView) findViewById(R.id.tvInfo);
Handler h = new Handler() {
  public void handleMessage(android.os.Message msg) {
    tvInfo.setText("received: " + msg.what);
  };
}; 

And implement some thread which will send messages to our handler somewhere else (in onClickListener for example):

Thread t = new Thread(new Runnable() {
    public void run() {
      for (int i = 1; i <= 10; i++) {
        // some long action
        downloadFile();
        h.sendEmptyMessage("downloading of file " + i + " complete");
      }
    }
});
t.start();

1 Comment

my question is more global, it's about message passing in the multithreading and not about how to use Handler in Android, sorry :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.