Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 13 June 2011

Multi-Threaded Execution Control

Posted on 04:06 by Unknown
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();
      }
}

Read More
Posted in | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Weekend at Yeouido (Han River Cruise)
    Yeouido is one of the Han River Cruise boarding points. It has a wonderful river side park (Yeouido P...
  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...
  • Few Multi-Threading Implementations
    Sequential Producer & Consumer In my first example, I will demonstrate an implementation to solve ...
  • Accessing Yahoo Finance API
    Since last few days I was wondering the right set of Web Service to read Country wise Stock Exchange index information . I found a bunch of ...
  • Regular Expression in JavaScript
    I was struggling to get familiar with JavaScript's Regular expression handling and thought my learning might help others. Here I'll...
  • Alphanumeric Random Number
    Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of ...
  • Binary Search Tree in Java
    This post is a part of my continuous effort to just implement few of the important data structures in ...
  • QuickSort in Java
    This post is a part of my continuous effort to just implement few of the important data structures in Java. Please find my previous post on ...
  • InDroid- Access to Android's Hidden API
    I have started one project " InDroid " to expose and share Hidden and Internal methods and variables of Android platform which are...
  • Image Geo Tagging for Android
    " onMap " is an Image Geo Tagging application, which can read EXIF header of images stored in your Android device and Map them ov...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ▼  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ▼  June (1)
      • Multi-Threaded Execution Control
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ►  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (4)
    • ►  June (7)
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile