Wednesday, November 25, 2015

Concurrent Programming in Android - Part II

Note : Here are my two tutorials on Android Concurrency Model. Please have a look at these:
In the part - I of the discussion on concurrent programming in Android, we have seen about the Handler-Message-Runnable & Asynchronous task framework. There we took a case study of downloading an image from an Internet server and found out how these methodologies can be applied for a long running task which has to be done in a background thread.

With the continuation of the same discussion we will today talk about the internals of the Android Concurrency Model - particularly about the topics like Looper, Message Queue, Handler and and how they work together to give us a better concurrent system. We will also develop an example in which we will pass a message from the main UI thread to a background worker thread immediately after the worker thread is created. We will also see the usefulness of the Java Synchronization technique, specifically the CountdownLatch synchronization object.

Here is a simplified diagram of Asynchronous Model of Android which shows how we can post Runnables or send Messages through Handler to the MQ of the Looper.

At the heart of any UI driven model, lies a loop called Message Loop whose task is to listen to any hardware events that the user may generate and dispatch them to the appropriate handler. In android this message looping is done by a class called Looper. The simpler version of the Looper class is as follws:

public class Looper {
    private static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    // Looper message queue
  final MessageQueue mQueue;
    // The current thread
    Thread mThread;
    // . . . Other attributes
    // The message queue for each Looper object, and it belongs to the thread
    private Looper() {
        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
    // Initialize the current thread as a looper.
    public static final void prepare() {
        if (sThreadLocal.get() != null) {
            // Try to have Looper thread again to create the Looper will throw an exception
            throw new RuntimeException("Only one Looper may be created per thread");
        sThreadLocal.set(new Looper());
    // Other methods 

Important function of Looper is prepare.

As we can see from the above code that the prepare function actually constructs the Message Queue responsible for queuing up messages. Also in the prepare function we can see that there will be only one looper per thread. The looper gets stored as a Thread Local Storage object. And as we can see, the thread in which the Looper.prepare is called, becomes the looper's mThread object.

The other important function of the Looper class is the loop method. It has been defined as follows:

//Run the message queue in this thread.
public static void loop() {
//Other code
final Looper me = myLooper();//returns the current looper
//other code......
 for (;;) {
            Message msg =; // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
//other code...........;
//other code....

Have a look infinite for loop and the following dispatchMessage method. This is what is responsible for listening events and dispatching those messages to the right handler.

Next important Class is the Handler class. The bare minimal version of this Handler class is as follows:
public class Handler {
    final MessageQueue mQueue;  // The associated MQ
    final Looper mLooper;  // The associated looper
    final Callback mCallback; 
    // Other attributes
    public Handler() {
//Other code ......
        // The default will be associated with the current thread looper
//returning the Thread Local storage's Looper
        mLooper = Looper.myLooper();
        // Looper cannot be empty, the construction method of the default can only be used on the looper thread
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
       //Associating the MQ with the Looper
        mQueue = mLooper.mQueue;
        mCallback = null;
    // Other methods

The handler class is a utility class and is responsible to help users to send messages to the Message queue. If we take the current thread's Handler to send message, the messages will be posted to the current thread's looper's Message Queue.

The main thread or the UI thread of Android will automatically have a looper. But if we want to create an worker thread, we need to specifically add the Looper for that thread. The below code is an example of such an worker thread. Lets call it as LooperThread.

class LooperThread extends Thread {
    public Handler mHandler;
    public void run() {
//Other works such as handler      
mHandler = new Handler() {
          public void handleMessage(Message msg) {
              // process incoming messages here

As we can see from the above code snippet, the moment we declare the above piece of code in any Android worker thread, it becomes capable of handling incoming messages.

Its all theories. Now lets write an example of sending messages from the UI thread of Android (or Activity) to a background thread. You can download the source code from here

The WorkerThread class is defined as follows:

 * Created by som on 24/11/15.
public class WorkerThread extends Thread{

    public Handler workerThreadHandler;
    CountDownLatch mLatch;

    public WorkerThread(CountDownLatch latch){

        mLatch = latch;

    public void run() {
        workerThreadHandler = new Handler() {
            public void handleMessage(Message msg) {

                Log.i("MsgToWorkerThread", "Message received from UI thread...");
                        MainActivity.getMainApp().runOnUiThread(new Runnable() {

                            public void run() {
                                Toast.makeText(MainActivity.getMainApp().getApplicationContext(), "Message received in worker thread from UI thread", Toast.LENGTH_LONG).show();
                                //Log.i("MsgToWorkerThread", "Message received from UI thread...");


        Log.i("MsgToWorkerThread", "Worker thread ready...");

 And the MainActivity from where the message is sent to the WorkerThread is defined as follows:

public class MainActivity extends AppCompatActivity {

    private static MainActivity mainApp;
    private CountDownLatch mCountDownLatch;

    protected void onCreate(Bundle savedInstanceState) {
///.....................Other Code......////
        mCountDownLatch = new CountDownLatch(1);
        mainApp = this;
        WorkerThread workerThread = new WorkerThread(mCountDownLatch);
        try {
            Log.i("MsgToWorkerThread", "Worker Thread is up and running. We can send message to it now...");
        } catch (InterruptedException e) {
        Toast.makeText(this, "Trial run...", Toast.LENGTH_LONG).show();
        Message msg = workerThread.workerThreadHandler.obtainMessage();
        //Toast.makeText(this, "Trial run...", Toast.LENGTH_LONG).show();

This is simple. Only one issue that i would like to discuss about that to avoid the Race Condition, i have used the Java Synchronization object CountdownLatch. For this countdownlatch, we send the message only when the worker thread becomes ready.
Hope i am able to throw some lights on the internals of Android Concurrency Model.

No comments: