Yes the synchronized locking synmtax is easy because of it's block structure but sometimes you can't use it. Is there a way to explicitly lock and unlock the "synchronized" monitor on an object so that it is compatible with prior usages of "synchronized" ?
class MyObj {
Object locker_ = new Object();
void lockedMethod() {
synchronized(locker_) {
....
}
}
Iterator lockTraversal() {
explicitlyWaitForAndAcquireLock(locker_); // assume will not throw exception
return(getAnIterator());
}
void unlockTraversal() {
explicitlyReleaselock(locker_);
}
}
MyObj obj = (...)
try {
Iterator it = obj.lockTraversal();
for(;;) // iterate
} finally {
obj.unlockTraversal();
}
Of course in this example "closures" would eliminate this need, As would "stack scoped" destructors ;^> But ...