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 block no such requirement
monitor is released monitor is not released
awake when notify() or notifyAll() method is called. not awake when notify() or notifyAll() method is called
not a static method static method
wait() is generally used on condition sleep() 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.

deadlock-condition

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:

Inter-thread communication

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

Previous
Next

2 Comments

  1. nagendra patel June 25, 2013
  2. Vovo Lviv February 10, 2014