In Java, what is the reason behind Object's wait() method being implemented as a final method? Is there no need for overriding wait()?
The Java Language Specification describes how synchronization works. Among the many concepts, like object monitors, there's also the concept of Wait Sets.
Every object, in addition to having an associated monitor, has an associated wait set. A wait set is a set of threads.
When an object is first created, its wait set is empty. Elementary actions that add threads to and remove threads from wait sets are atomic. Wait sets are manipulated solely through the methods
Object.wait
,Object.notify
, andObject.notifyAll
.
If you were able to override the method, you might find yourself with a way to break synchronization. Java can not allow that. It has to have control over the exact implementation, which is why it is final
.
We call wait(), notify() or notifyAll method in Java from synchronized method or synchronized block in Java to avoid:
Hence, I guess it would not be a good idea to let override wait()
See more in Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With