Inter-thread communication

Inter Thread Communication is one of the distinct facility in multi threading application development of java. Inter thread communication concept is one of the specialized form of inter process communication of operating system. In real world Inter Thread communication based applications of java very fastest applications compared to all the applications in java.

Cooperation(Inter-thread communication) is all about making synchronized threads communicate with each other. Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class:
  • wait()
  • notify()
  • notifyAll()
  1. wait() tells calling thread to give up monitor and go to sleep until some other thread enters the same monitor and call notify. Causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. The current thread must own this object's monitor.                                         Syntax:
    public final void wait() throws InterruptedException
    public final void wait(long timeout)throws InterruptedException
    
  2. notify() wakes up a thread that called wait() on same object. Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.
    Syntax:

    public final void notify()
    
  3. notifyAll() wakes up all the thread that called wait() on same object. Wakes up all threads that are waiting on this object's monitor.
    public final void notifyAll()
    

Difference between wait() and sleep()


wait()sleep()
called from synchronized blockno such requirement
monitor is releasedmonitor is not released
awake when notify() or notifyAll() method is called.not awake when notify() or notifyAll() method is called
not a static methodstatic method
wait() is generally used on conditionsleep() method is simply used to put your thread on sleep.
Thread Pooling

Pooling is usually implemented by loop i.e to check some condition repeatedly. Once condition is true appropriate action is taken. This waste CPU time.

Deadlock:
Deadlock is a situation of complete Lock, when no thread can complete its execution because lack of resources. In the above picture, Thread 1 is holding a resource R1, and need another resource R2 to finish execution, but R2 is locked by Thread 2, which needs R3, which in turn is locked by Thread 3. Hence none of them can finish and are stuck in a deadlock.


Example of Inter thread Communication:
class Customer {
   int amount=0;
   int flag=0;
   public synchronized int withdraw(int amount){
 System.out.println(Thread.currentThread().getName()+" is going to withdraw");
 
       if(flag==0){
           try{
  System.out.println("waiting....");
  wait();
      }catch(Exception e){}
 }
 this.amount-=amount;
 System.out.println("withdraw completed");
 return amount;
     }

     public synchronized void deposit(int amount){
 System.out.println(Thread.currentThread().getName()+" is going to  deposit");
 this.amount+=amount;
 System.out.println("deposit completed");
        notifyAll();
        flag=1;
    }
 }

public class SyncThreadDemo {
    public static void main(String[] args) {
 final Customer c = new Customer();
 
 Thread t1 = new Thread(){
  public void run(){
   c.withdraw(5000);
   System.out.println("After withdraw amount is "+c.amount);
                        
  }
   };
 
 Thread t2 = new Thread(){
  public void run(){
   c.deposit(9000);
   System.out.println("After deposit amount is "+c.amount);
  }
    };

 t1.setName("Dinesh");
        t2.setName("Sweety");
 t1.start();
 t2.start();

    }
}


Output:



<<Previous <<   || Index ||   >>Next >>

2 comments:

  1. i have one question asked in an interviewer suppose there are tree thread printing natural number like that....
    Th1=2,5,7
    th2=1,3,6
    th1=4,8,9
    So how to synchronize like output:1,2,3,4,5,6,7,8

    ReplyDelete
    Replies
    1. Will this solution do?

      class Model {
      int nextValue = 1;

      void print(int[] arr) {
      for (int i : arr) {
      if (i == nextValue) {
      synchronized (this) {
      System.out.print(nextValue++ + " ");
      }
      }
      }
      }
      }

      class Thread1 extends Thread {
      int[] arr = { 2, 5, 7 };
      private Model model;

      public Thread1(Model model) {
      this.model = model;
      }

      @Override
      public void run() {
      while (true) {
      model.print(arr);
      }
      }
      }

      class Thread2 extends Thread {
      int[] arr = { 1, 3, 6 };
      private Model model;

      public Thread2(Model model) {
      this.model = model;
      }

      @Override
      public void run() {
      while (true) {
      model.print(arr);
      }
      }
      }

      class Thread3 extends Thread {
      int[] arr = { 4, 8, 9 };
      private Model model;

      public Thread3(Model model) {
      this.model = model;
      }

      @Override
      public void run() {
      while (true) {
      model.print(arr);
      }
      }
      }

      public class SynchTask {
      public static void main(String[] args) {
      Model model = new Model();
      Thread1 thread1 = new Thread1(model);
      Thread2 thread2 = new Thread2(model);
      Thread3 thread3 = new Thread3(model);
      thread1.start();
      thread2.start();
      thread3.start();
      }
      }

      Delete