I have been thinking of send a proposal to the Java language architects.
In a synchronized block
synchronized(lock) {
// If there is no notification before this point
// <--- implicitly put here // lock.notifyAll(); // OR // lock.notify();
}
After a thread left synchronized block, it cannot call lock.notifyAll() / lock.notify() anymore without getting exception.
Forgetting to notify other thread monitor holders may forever make them (other threads) wait (unless, they put some timeout in their wait method).
synchronized(lock) {
lock.wait(); //<--- this thread may forever freeze here
}
I cannot imagine a situation in which such behavior (inserting implicit notification at the end of a synchronized block, when there is no explicit notification) is undesirable.
The same approach can be applied to synchronized methods.
There can be different ways how to (technically) implement such behavior, for example:
@autonotify
synchronized(lock) {
...
}
@autonotify
public void synchronized doSomething() {
...
}
Or:
@autonotifyAll
synchronized(lock) {
...
}
@autonotifyAll
public void synchronized doSomething() {
...
}
Or - make auto-notification the default behavior, but leaving ability to suppress it, for example:
@suppressautonotify
synchronized(lock) {
...
}
@suppressautonotifyAll
public void synchronized doSomething() {
...
}
What do you think? Objections?
The best commentary for or against the proposal will be accepted as the answer.