How can the wait() and notify() methods be called on Objects that are not threads?
Locking is about protecting shared data.
The lock is on the data structure being protected. The threads are the things accessing the data structure. The locks are on the data structure object in order to keep the threads from accessing the data structure in an unsafe way.
Any object can be used as an intrinsic lock (meaning used in conjunction with synchronized
). This way you can guard access to any object by adding the synchronized modifier to the methods that access the shared data.
The wait
and notify
methods are called on objects that are being used as locks. The lock is a shared communication point:
When a thread that has a lock calls
notifyAll
on it, the other threads waiting on that same lock get notified. When a thread that has a lock callsnotify
on it, one of the threads waiting on that same lock gets notified.When a thread that has a lock calls
wait
on it, the thread releases the lock and goes dormant until either a) it receives a notification, or b) it just wakes up arbitrarily (the "spurious wakeup"); the waiting thread remains stuck in the call to wait until it wakes up due to one of these 2 reasons, then the thread has to re-acquire the lock before it can exit the wait method.
See the Oracle tutorial on guarded blocks, the Drop class is the shared data structure, threads using the Producer and Consumer runnables are accessing it. Locking on the Drop object controls how the threads access the Drop object's data.
Threads get used as locks in the JVM implementation, application developers are advised to avoid using threads as locks. For instance, the documentation for Thread.join says:
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Java 5 introduced explicit locks implementing java.util.concurrent.locks.Lock
. These are more flexible than the implicit locks; there are methods analogous to wait and notify (await and signal), but they are on the Condition, not on the lock. Having multiple conditions makes it possible to target only those threads waiting for a particular type of notification.
You can use wait()
and notify()
to synchronize your logic. As an example
synchronized (lock) {
lock.wait(); // Will block until lock.notify() is called on another thread.
}
// Somewhere else...
...
synchronized (lock) {
lock.notify(); // Will wake up lock.wait()
}
with lock
being the class member Object lock = new Object();
Think using a real life example, a washroom. When you want to use the washroom at your office, you have two options to make sure no one else will come to the washroom once you are using it.
- Lock the washroom door, so everyone else will know that it's used by someone else when they try to open the door
- Go to each person in the office, lock them to their chairs (or table, or whatever), go to washroom.
Which option would you take?
Yes, it's the same in the Javaland!.
So in the above story,
- Washroom = Object you want to lock (that only you need to use)
- Your staff colleagues = other threads that you want to keep out
So just like in real life, when you have some private business, you lock that object. And when you are done with that object, you let go of the lock!.
(Yes yes!, this is a very simple description on what happens. Of course the real concept is slightly different from this, but this is a starting point)