Inter Thread Communication (Cooperation): Multi Threading In Java

//Inter Thread Communication
// It is all about allowing synchronised threads to communicate with each.
//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()
// wait() Method: causes current thread to release a lock and wait until either another thread invokes the notify method
// notifyAll() method for this object or a specified time is elapsed.
//notify() method: wakes up the 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()
//notifyAll() method: Wakes up all threads that are waiting on this object monitor. Syntax:
//public final void notifyAll()
// Program Example:

public class InterThreadCommunication extends Thread {

int amount=10000;

public void run() {

synchronized void withdraw(int amount){
System..println("Going to withdraw rs .. "+amount);

System..println("Less balance, Waiting for deposit .. "+this.amount);
try {
} catch (InterruptedException e) {
System..println("Withdraw complete of rs .."+amount);
System..println("Balance left after withdraw .. "+this.amount);

synchronized void deposit(int amount){
System..println("going to deposit rs .."+amount);
System..println("total balance after deposit in rs .. "+this.amount);

public static void main(String[] args) {

InterThreadCommunication interThreadCommunication1=new InterThreadCommunication();




Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store