For synchronized access on an object, the following pattern is common:
Object lock = new Object();
@GuardedBy("lock")
Object sharedObject;
Where lock is used as synchronized(lock) {}. The annotation is nice but it's not enforced. I would like to do something like this:
class SyncedReference<T> {
Object lock;
T value;
SyncedReference(Object lock) {}
T get() {
if (!isLocked(lock)) {
throw new IllegalStateException();
}
return value;
}
boolean isLocked() {
// How to do this?
}
}
My question is how to implement the isLocked() method?