Case Study-IA4-OOC
Case Study-IA4-OOC
When should you use the wait() and notify method in Java is one of the
many popular questions about the wait and notify methods from Java
multithreading interview questions. One of the reasons for its popularity is
that still a lot of Java programmers struggle to explain and write code using
wait-notify methods. Many Java developer only knows some facts about the
wait and notify methods like that wait() and notify() are defined in
the java.lang.Object class or you cannot call wait() without
synchronization, which means without a synchronized block or synchronized
method but doesn't really know when and how to use them.
In this article, we will try to bridge that gap by going through a simple example
of classical Producer-Consumer problems and show you how you can use
them while writing concurrent Java applications for the real world.
If you don't know much about them, I suggest you join a fundamental course
on Java threads like Multithreading and Parallel Computing in
Java course from Udemy. It's also very affordable and you can get in just $9.9 on
Udemy sales.
In this example, you will learn how to implement multiple producers and
single consumer solutions using wait() and notify() in Java. If you are
not familiar with notify and notifyAll method, you can further see The
Complete Java Masterclass to learn more.
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
/**
* A simple Java Program to demonstrate how to use wait
* and notify() method ofr inter-thread communciation
* in Java.
*/
}
}
while (!bExit) {
synchronized (sharedQueue) {
while (!sharedQueue.isEmpty()) {
String item = sharedQueue.poll();
System.out.println("Consumer removed : " + item);
System.out.println("Consumer notifying Producer: " + item);
sharedQueue.notify();
}
}
}
}
}
Output:
Producer added : 12275948008616
Producer sleeping by calling wait: 12275948008616
Consumer removed : 12275948008616
Consumer notifying Producer: 12275948008616
Producer wake up:
Producer added : 12275948047960
Producer sleeping by calling wait: 12275948047960
Consumer removed : 12275948047960
Consumer notifying Producer: 12275948047960
Producer wake up:
Producer added : 12275948082600
Producer sleeping by calling wait: 12275948082600
Consumer removed : 12275948082600
Consumer notifying Producer: 12275948082600
That's why you see an ordered output like Producer added, Producer
Sleeping, Consumer Removed, Consumer Notified, and Producer Wakeup.
In short, both Producer and Consumer Thread are talking with each other
using the wait and notify method.
If you remove that wait call then the Producer thread will keep checking for
the queue to become waiting and keep wasting the CPU cycle.
Similarly, if you remove the notify call then the waiting Producer thread may
never wake up. Btw, if you have trouble understanding Producer-Consumer
Problem then I also suggest taking a look at the
Applying Concurrency and Multi-threading to Common Java
Patterns course on Pluralsight.