I'm wondering if there is an easy way to make a synchronized
lock that will respond to changing references. I have code that looks something like this:
private void fus(){
synchronized(someRef){
someRef.roh();
}
}
...
private void dah(){
someRef = someOtherRef;
}
What I would like to happen is:
- Thread A enters
fus
, and acquires a lock onsomeref
as it callsroh()
. Assumeroh
never terminates. - Thread B enters
fus
, begins waiting for someRef` to be free, and stays there (for now). - Thread C enters
dah
, and modifiessomeRef
. - Thread B is now allowed to enter the synchronized block, as
someRef
no longer refers to the object Thread A has a lock on.
What actually happens is:
- Thread A enters
fus
, and acquires a lock onsomeref
as it callsroh()
. Assumeroh
never terminates. - Thread B enters
fus
, finds the lock, and waits for it to be released (forever). - Thread C enters
dah
, and modifiessomeRef
. - Thread B continues to wait, as it's no longer looking at
someref
, it's looking at the lock held by A.
Is there a way to set this up such that Thread B will either re-check the lock for changing references, or will "bounce off" into other code? (something like sychronizedOrElse?)