Thursday, 6 September 2018

Threads: print characters of a string simultaneously



As you see above image, two threads must simultaneously prints the data simultaneously.

Thread1 prints 1st character of string, thread2 print 2nd character of string, thread1 prints 3rd character and so on…

We can achieve this using wait and notify methods of object class.
Assume data = "Hello World";

In this example, procedure is like below.
a.   Initialize the counter to 0, set the flag to true
b.   For thread1, acquire the lock on ‘data’ object. If flag is true, then print the data[counter], increment the counter and release the lock and notify the threads before releasing. If flag is false, then release the lock by calling wait method on data object.
c.    For thread2, acquire the lock on ‘data’ object. If the flag is false, then print the data[counter], increment the counter and release the lock and notify the thread2 before releasing. If flag is tur, then release the local by calling wait method on data object.

Find the below working example.

Test.java
package com.sample.arrays;

public class Test {
 private static final String data = "Hello World";
 private static boolean flag = true;
 private static int count = 0;

 private static Thread t1 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (!flag) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread1 =>" + data.charAt(count++));
      flag = false;
      data.notify();
     }

    }
   }
  }
 };

 private static Thread t2 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (flag) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread2 =>" + data.charAt(count++));
      flag = true;
      data.notify();
     }

    }
   }
  }
 };

 public static void main(String args[]) {
  t1.start();
  t2.start();
 }
}


Enhancing above application. Since thread1 print the characters at even positions and thread2 print the characters at odd positions. We can use the counter variable itself to achieve this behavior.


Test.java
package com.sample.arrays;

public class Test {
 private static final String data = "Hello World";
 private static int count = 0;

 private static Thread t1 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (count % 2 != 0) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread1 =>" + data.charAt(count++));
      data.notify();
     }

    }
   }
  }
 };

 private static Thread t2 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (count % 2 == 0) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread2 =>" + data.charAt(count++));
      data.notify();
     }

    }
   }
  }
 };

 public static void main(String args[]) {
  t1.start();
  t2.start();
 }
}

Let’s enhance this by extending it for three threads.


Test.java

package com.sample.arrays;

public class Test {
 private static final String data = "Hello World";
 private static int count = 0;

 private static Thread t1 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (count % 3 != 0) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread1 =>" + data.charAt(count++));
      data.notifyAll();
     }

    }
   }
  }
 };

 private static Thread t2 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (count % 3 != 1) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread2 =>" + data.charAt(count++));
      data.notifyAll();
     }

    }
   }
  }
 };
 
 private static Thread t3 = new Thread() {
  int dataLength = data.length();

  public void run() {
   while (count < dataLength) {
    synchronized (data) {
     if (count % 3 != 2) {
      try {
       data.wait();
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {
      System.out.println("Thread3 =>" + data.charAt(count++));
      data.notifyAll();
     }

    }
   }
  }
 };

 public static void main(String args[]) {
  t1.start();
  t2.start();
  t3.start();
 }
}

Previous                                                 Next                                                 Home

No comments:

Post a Comment