Inter-thread communication in Java

Inter-thread communication in Java

What is Inter-thread communication?

When two threads will communicate with each other is called Inter-thread communication.
Two threads will communicate with each other by using wait(), notify(), notifyAll() methods.

wait(), notify(), notifyAll() methods are available in Object class, but not in threads class. Because the thread is required to call these methods on any shared object.

wait() Method:

The method which is required to update has to call the wait() method, the thread which is responsible to update it has to call the notify() method.

If a thread call wait() method releases the lock immediately and enters into the waiting state.

notify() Method :

We can use the notify() method to notify(wakes up) only one waiting thread, that called wait() on the same object. If multiple threads are waiting on this object, one of them is chosen to be notified (awakened), but which waiting thread will be notified we can’t expect exactly. All remaining threads have to wait for the next notification.

Syntax:

public final void notify()

notifyAll() Method:

In the notifyAll() method all waiting threads will be notifying(wakes up) which is waiting on the same object. But threads will be executed one by one.

Syntax:

public final void notifyAll()

If a thread wants to call wait(), notify(), notifyAll() methods compulsory the thread should be the owner of the object.

The thread has to get a lock of that object, which means the threads should be in the Synchronization area.

If we call wait(), notify(), notifyAll() method from outside of Synchronization area, then we will get RuntimeException and that exception is EllegalMonitoryStateException.

EllegalMonitoryStateException 

public final void wait()throws InterruptedException
public final void wait(long timeout)throws InterruptedException
public final void wait(long ms, int ms)throws InterruptedException
public final native void notify()throws InterruptedException
public final native void notifyAll()throws InterruptedException

Only using the wait(), notify(), notifyAll() method thread can release the lock, there is no other option for the thread that can release the lock.

Programming Example of wait(), notify() method.

package com.technocalRound;

class User{
int amount = 5000;

synchronized void withdraw(int amount) {
System.out.println("withdraw Process start...");

if (this.amount < amount) {
System.out.println("Less Amount, please deposit Amount...");
try {
wait();
} catch (Exception e) {
}
}
this.amount -= amount;
System.out.println("Your withdraw process completed..");
}

synchronized void deposit(int amount) {
System.out.println("deposit Process start...");
this.amount += amount; 
System.out.println("Your deposit process completed... ");
notify();
}
}

class Example2 {
public static void main(String args[]) {
final User u = new User();
new Thread() {
public void run() {
u.withdraw(10000);
}
}.start();
new Thread() {
public void run() {
u.deposit(5000);
}
}.start();

}
}

Output:

withdraw Process start...
Less Amount, please deposit Amount...
deposit Process start...
Your deposit process completed... 
Your withdraw process completed..

Produce and Consumer

Produce:

The producer has to produce items in the queue. After producing the items, he has to call notify() method, so all that consumers will be notified.

synchronized(q)
{
update iteams;
q.notify();
}

Consumer:

The consumer has to consume items from the queue. If the queue is empty he has to call wait() method.

synchronized(q)
{
if(q is empty){
q.wait();
}
consumer iteam;
}

Read More topics related to the collection.

Read more topics related to java

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *