I have the following code for locking an object with a particular user ID
public boolean acquireLock(Long id) {
if (lock.compareAndSet(0L, id)) {
return true ;
}
return false ;
}
I acquire it in the following way:
while(!parent.acquireLock(id)){
System.out.println(lock.get());
if (count++>1000000) {
System.out.println(id + " Trying to acquire " + lock.get());
DebugHandler.createException("Error, deadlock");
}
}
Release it as :
public boolean releaseLock(Long id) {
if (lock.compareAndSet(id, 0)) {
System.out.println("Releasing Lock for " + id);
return true ;
}
else {
DebugHandler.createException("Lock not owned by current view. Thief");
return false ;
}
}
And declare the lock object as:
private volatile AtomicLong lo = new AtomicLong(0);
except that I get the following odd behaviour and deadlock, which concludes with:
Id 45 trying to acquire 0
Ak, the value of the lock is systematically 0 but the compare and swap test fails, believing it isn't 0. (the counter to test for deadlock is reinitialised after I exit the loop)
Any ideas?