First off, thank you for the book, its organization and set of problems! This is my first post to the forum and I can’t thank you guys enough.
Here is the problem 10.17 as seen on my Google Play Version 1.6.1 of EPI (Java):
The given solution however does not seem to provide a good API. Here is how a (single threaded!) caller is supposed to call it:
if (!node.isLocked()) {
if (node.lock()) {
// only now is it locked, which means isLocked call was not useful
}
}
It’s a disaster if the caller forgets to do if (node.lock())
and simply does node.lock()
ignoring the return value (thinking that the check for isLocked()
already says that it is not locked. So, the expectation on the caller’s part is that the call to lock()
would succeed in locking the node.
Wouldn’t it be better if we had only lock()
and unlock()
methods to avoid this problem?
Thanks!