Thursday, October 19, 2017

Call in order -Multithreading


/* Problem Statement- Call in order...
 * threadA calling first(), threadB calling second()
 * and threadC calling third()
 */


Summary - Even if thread2/thread3 enters run() , they will relinquish the lock and thread1 would be the first who at last pick the lock and satisfy the condition and make other threads to proceed accordingly



import java.util.concurrent.atomic.AtomicInteger;

class Foo implements Runnable {

     AtomicInteger count=new AtomicInteger(1);
     public final static Object obj = new Object();

     public void first() {
          System.out.println("*******First Method called by*****" + Thread.currentThread().getName());
     }

     public void second() {
          System.out.println("*******Second Method called by******" + Thread.currentThread().getName());
     }

     public void third() {
          System.out.println("*******Third Method called by******" + Thread.currentThread().getName());
     }

     public void run() {
          synchronized (obj) {
              System.out.println("Lock acquired by " + Thread.currentThread().getName());
              while (count.get() <= 3) {
                   if (Thread.currentThread().getName().equals("Thread A") && count.get() == 1) {
                        first();
                        count.incrementAndGet();
                        obj.notifyAll();
                   } else if (Thread.currentThread().getName().equals("Thread B") && count.get() == 2) {
                        second();
                        count.incrementAndGet();
                        obj.notifyAll();
                   } else if (Thread.currentThread().getName().equals("Thread C") && count.get() == 3) {
                        third();
                        count.incrementAndGet();
                        obj.notifyAll();
                   } else {
                        try {
                             System.out.println("Lock released by " + Thread.currentThread().getName());
                             obj.wait();
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   }
              }
          }
     }
}

public class CallInOrder {
     public static void main(String[] args) {
          Foo fooInst = new Foo();
          Thread threadA = new Thread(fooInst, "Thread A");
          Thread threadB = new Thread(fooInst, "Thread B");
          Thread threadC = new Thread(fooInst, "Thread C");
          threadC.start();
          threadB.start();
          threadA.start();
     }
}

Output

Lock acquired by Thread C
Lock released by Thread C
Lock acquired by Thread B
Lock released by Thread B
Lock acquired by Thread A
*******First Method called by*****Thread A
Lock released by Thread A
*******Second Method called by******Thread B
Lock released by Thread B
*******Third Method called by******Thread C

1 comment:

  1. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
    http://chennaitraining.in/qliksense-training-in-chennai/
    http://chennaitraining.in/pentaho-training-in-chennai/
    http://chennaitraining.in/machine-learning-training-in-chennai/
    http://chennaitraining.in/artificial-intelligence-training-in-chennai/
    http://chennaitraining.in/snaplogic-training-in-chennai/
    http://chennaitraining.in/snowflake-training-in-chennai/

    ReplyDelete