Monday, June 13, 2011

Multi-Threaded Execution Control

In this tutorial I'll explain mechanism to control multiple thread execution sequence. To explain the concept, I'll consider a sample execution scenario where a set of operations execute in sequence, but each operation runs in different threads. Lets consider content download scenario, Login to the Content Server -> Browse Content -> Download Content. 3 Threads will execute each of these operations and these operations are inter dependent and will follow the order, login - browse - download.

Let's jump into the implementation, we'll have Synchronized blocks for 3 different functions and we'll do thread execution control using wait()/notifyAll() methods. To determine the order of execution we'll use one variable state which can hold 3 different values- Login, Browse and Download.

Synchronization blocks will be locked with a Private Object lock. This is more effective and fail proof then using the this as Object lock. All 3 functions will share same lock, so any thread can execute only one function at a time.
When one thread is done with its execution, it needs to notify all other threads by calling lock.notifyAll() and release the lock by calling lock.wait(). Apart from this the currently executing thread should also change the value of state which will indicate who will be the next thread or function to execute.
I have written 3 functions, login(), browse() and download() as part of Operation class and all these 3 functions share lock of same private object. MyThread class is a thread implementation and there are 3 thread instances, each executes only one of the 3 functions.

Output of the following code-

login....
login.....[DONE]
browse....
browse.....[DONE]
download....
download.....[DONE]
login....
login.....[DONE]
browse....
browse.....[DONE]
download....
download.....[DONE]


package com.ds.thread;

/**
 * Multi-Threaded sequential execution control.
 *
 * @author prasanta
 *
 */
public class SequentialThreadAccess {

      /**
       * Operation class executes 3 different functions- Login -> Browse -> Download
       * Note:
       * Make sure you run wait() and notifyAll() on the Object, whose lock
       * you are using. In this case, Object lock, so
       * lock.wait() and lock.notifyAll().
       *
       * @author prasanta
       *
       */
      private static class Operation {
           
            public static final int LOGIN = 0;
            public static final int BROWSE = 1;
            public static final int DOOWNLOAD = 2;
           
            int state = LOGIN;
           
            // My Lock object
            private final Object lock = new Object();
           
            public void login()
            {
                  synchronized (lock) {
                        while(true){
                              while(state != LOGIN){
                                    try {
                                          // Release the Lock
                                          lock.wait();
                                    } catch (InterruptedException e) {
                                          e.printStackTrace();
                                    }
                              }
                             
                              System.out.println("login....");
                              try{
                                    // some processing delay
                                    Thread.sleep(1000);
                              }catch(Exception ex){}
                              System.out.println("login.....[DONE]");
                             
                             
                              // I'm done, let Browsing to run
                              state = BROWSE;
                              lock.notifyAll();
                        }
                  }// Synchronized Section- END
            }
           
            public void browse()
            {
                  synchronized (lock) {
                        while(true){
                              while(state != BROWSE){
                                    try {
                                          // Release the Lock
                                          lock.wait();
                                    } catch (InterruptedException e) {
                                          e.printStackTrace();
                                    }
                              }
                             
                              System.out.println("browse....");
                              try{
                                    // some processing delay
                                    Thread.sleep(1000);
                              }catch(Exception ex){}
                              System.out.println("browse.....[DONE]");
                             
                              state = Operation.DOOWNLOAD;
                              // I'm done, let Downloading to run
                              lock.notifyAll();
                        }
                       
                  }// Synchronized Section- END
            }
           
            public void download()
            {
                  synchronized (lock) {
                        while(true){
                              while(state != DOOWNLOAD){
                                    try {
                                          // Release the Lock
                                          lock.wait();
                                    } catch (InterruptedException e) {
                                          e.printStackTrace();
                                    }
                              }
                             
                              System.out.println("download....");
                              try{
                                    // some processing delay
                                    Thread.sleep(1000);
                              }catch(Exception ex){}
                              System.out.println("download.....[DONE]");
                             
                              state = LOGIN;
                              // I'm done, let Login to run
                              lock.notifyAll();
                        }
                  }// Synchronized Section- END
            }
      }
     
      public static class MyThread extends Thread
      {
            int opCode = Operation.LOGIN;
            Operation op;
           
            public MyThread(String name, int opCode, Operation op){
                  super(name);
                  this.opCode = opCode;
                  this.op = op;
            }
           
            public void run(){
                  while(true){
                        switch(opCode){
                        case Operation.LOGIN:
                              op.login();
                        break;
                        case Operation.BROWSE:
                              op.browse();
                        break;
                        case Operation.DOOWNLOAD:
                              op.download();
                        break;
                        }
                  }
            }
      }
      public static void main(String[] args){
            //fiboSeq(15);
           
            Operation op = new Operation();
           
            MyThread th1 = new MyThread("TH1", Operation.LOGIN, op);
            MyThread th2 = new MyThread("TH2", Operation.BROWSE, op);
            MyThread th3 = new MyThread("TH3", Operation.DOOWNLOAD, op);
           
            th1.start();
            th2.start();
            th3.start();
      }
}