Which Class Do the wait(), notify(), and notifyAll() Methods Belong To?
Tuanh.net
3 min read
1. Understanding the wait(), notify(), and notifyAll() Methods
The wait(), notify(), and notifyAll() methods are integral to Java's concurrency model. They belong to the Object class, which is the root of the class hierarchy in Java. This means that every class in Java inherits these methods from the Object class.
1.1 The Object Class
The Object class is the superclass of all classes in Java. It provides a set of basic methods that every class inherits, including toString(), equals(), and hashCode(). The wait(), notify(), and notifyAll() methods are also part of this class, enabling threads to communicate and coordinate their activities.
1.2 The Role of wait(), notify(), and notifyAll()
- wait(): This method causes the current thread to wait until another thread invokes notify() or notifyAll() on the same object. It must be called from within a synchronized block or method.
- notify(): This method wakes up a single thread that is waiting on the object's monitor (the thread's lock). If multiple threads are waiting, one of them is chosen arbitrarily.
- notifyAll(): This method wakes up all threads that are waiting on the object's monitor. This is useful when multiple threads need to be informed about a change in state.
2. Using wait(), notify(), and notifyAll() in Practice
To understand how these methods work, let's look at some practical examples.
2.1 Example Code
Here’s a simple example demonstrating the use of these methods:
class SharedResource {
private boolean available = false;
public synchronized void consume() throws InterruptedException {
while (!available) {
wait(); // Wait until the resource is available
}
// Consume the resource
System.out.println("Resource consumed.");
available = false;
notify(); // Notify that the resource is now unavailable
}
public synchronized void produce() {
// Produce the resource
available = true;
System.out.println("Resource produced.");
notify(); // Notify that the resource is available
}
}
public class Main {
public static void main(String[] args) {
SharedResource resource = new SharedResource();
Thread producer = new Thread(() -> {
try {
while (true) {
Thread.sleep(1000); // Simulate time to produce
resource.produce();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumer = new Thread(() -> {
try {
while (true) {
resource.consume();
Thread.sleep(2000); // Simulate time to consume
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
producer.start();
consumer.start();
}
}
2.2 Demo Results
In the above example:
- The producer thread will periodically produce a resource and notify the consumer.
- The consumer thread will wait until the resource is available, consume it, and then notify the producer if needed.
You will see the following output indicating the producer and consumer operations:
Resource produced.
Resource consumed.
...
This output demonstrates how wait(), notify(), and notifyAll() coordinate the producer-consumer interaction.
3.Conclusion
By understanding which class the wait(), notify(), and notifyAll() methods belong to and how they work, you can effectively manage inter-thread communication in your Java applications. These methods are essential for ensuring that threads cooperate and share resources efficiently.
If you have any questions or need further clarification, feel free to leave a comment below!
Read more at : Which Class Do the wait(), notify(), and notifyAll() Methods Belong To?
0
Subscribe to my newsletter
Read articles from Tuanh.net directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by