Lock object which grants and releases locks, and associates locks with
owners. The methods to acquire and release a lock require an owner (Object). When a lock is acquired, we store the current owner with the lock. When the same owner (
but possibly running in a different thread) wants to acquire the lock, it is immediately granted. When an owner different from the one currently owning the lock wants to release the lock, we do nothing (no-op).
Consider the following example:
FIFOSemaphore lock=new FIFOSemaphore(1); lock.acquire(); lock.acquire(); lock.release();
This would block on the second
acquire() (although we currently already hold the lock) because the lock only has 1 permit. So
IdentityLock will allow the following code to work properly:
IdentityLock lock=new IdentityLock(); lock.readLock().acquire(this, timeout); lock.writeLock().acquire(this, timeout); lock.release(this);
If the owner is null, then the current thread is taken by default. This allow the following code to work:
IdentityLock lock=new IdentityLock(); lock.readLock().attempt(null, timeout); lock.writeLock().attempt(null, timeout); lock.release(null);
Note that the Object given as owner is required to implement {@link Object#equals}. For a use case see the examples in the testsuite.
@author
Bela Ban Apr 11, 2003
@author Ben Wang July 2003
@deprecated will be removed when we drop support for Pessimistic Locking and Optimistic Locking