Product Documentation : Ehcache Developer Guide : Using Explicit Locking : The Locking API
The Locking API
The following methods are available on Cache and Ehcache.
/**
* Acquires the proper read lock for a given cache key
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
*/
public void acquireReadLockOnKey(Object key) {
this.acquireLockOnKey(key, LockType.READ);
}
/**
* Acquires the proper write lock for a given cache key
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
*/
public void acquireWriteLockOnKey(Object key) {
this.acquireLockOnKey(key, LockType.WRITE);
}
/**
* Try to get a read lock on a given key. If can't get it in timeout millis
* then return a boolean telling that it didn't get the lock
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
* @param timeout - millis until giveup on getting the lock
* @return whether the lock was awarded
* @throws InterruptedException
*/
public boolean tryReadLockOnKey(Object key, long timeout) throws
InterruptedException {
Sync s = getLockForKey(key);
return s.tryLock(LockType.READ, timeout);
}
/**
* Try to get a write lock on a given key. If can't get it in timeout millis
* then return a boolean telling that it didn't get the lock
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
* @param timeout - millis until giveup on getting the lock
* @return whether the lock was awarded
* @throws InterruptedException
*/
public boolean tryWriteLockOnKey(Object key, long timeout) throws
InterruptedException {
Sync s = getLockForKey(key);
return s.tryLock(LockType.WRITE, timeout);
}
/**
* Release a held read lock for the passed in key
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
*/
public void releaseReadLockOnKey(Object key) {
releaseLockOnKey(key, LockType.READ);
}
/**
* Release a held write lock for the passed in key
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
*/
public void releaseWriteLockOnKey(Object key) {
releaseLockOnKey(key, LockType.WRITE);
}
/**
* Returns true if a read lock for the key is held by the current thread
*
* @param key
* @return true if a read lock for the key is held by the current thread
*/
boolean isReadLockedByCurrentThread(Object key);
/**
* Returns true if a write lock for the key is held by the current thread
*
* @param key
* @return true if a write lock for the key is held by the current thread
*/
boolean isWriteLockedByCurrentThread(Object key);
Copyright © 2010-2015 Software AG, Darmstadt, Germany.

Product Logo |   Feedback