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 update it 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 it releases lock immediately and enters into the waiting state.

 

notify() Method :

We can use 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 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 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;
}

 

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

 

 

12 Replies to “Inter-thread communication in Java”

  1. I wanted to put you the very little remark to be able to thank you the moment again over the remarkable tactics you have provided on this page. This has been simply surprisingly open-handed with you to offer unhampered precisely what most of us would’ve offered for sale for an ebook to help make some money on their own, particularly seeing that you might have tried it in case you desired. Those strategies in addition worked to become good way to know that some people have the identical keenness really like my personal own to realize much more when it comes to this matter. I believe there are several more pleasant instances ahead for folks who check out your blog.

  2. I am only writing to make you understand what a superb experience our princess undergone viewing yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  3. A lot of thanks for all your valuable efforts on this site. Kate loves engaging in investigation and it’s really easy to understand why. I know all concerning the dynamic medium you give important secrets on this web blog and as well as foster participation from others on that content plus our daughter is always discovering a lot. Have fun with the remaining portion of the new year. You are conducting a great job.

  4. I happen to be writing to make you understand what a terrific experience our princess went through visiting yuor web blog. She realized such a lot of things, with the inclusion of what it is like to possess an incredible teaching style to make the others without hassle thoroughly grasp various tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those warm and friendly, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  5. I am just writing to make you understand what a perfect experience my wife’s girl obtained using your webblog. She noticed so many pieces, which included what it is like to possess an excellent helping spirit to let others with no trouble learn about specific specialized things. You truly surpassed readers’ expected results. Thank you for providing these priceless, trusted, informative as well as fun tips about this topic to Lizeth.

Leave a Reply

Your email address will not be published.