org.apache.commons.transaction.locking
Class ReadWriteLock
java.lang.Object
org.apache.commons.transaction.locking.GenericLock
org.apache.commons.transaction.locking.ReadWriteLock
- All Implemented Interfaces:
- MultiLevelLock, MultiLevelLock2
public class ReadWriteLock
- extends GenericLock
Convenience implementation of a read/write lock based on GenericLock
.
Reads are shared which means there can be any number of concurrent read
accesses allowed by this lock. Writes are exclusive. This means when there is
a write access no other access neither read nor write are allowed by this
lock. Additionally, writes are preferred over reads in order to avoid starvation. The idea
is that there are many readers, but few writers and if things work out bad the writer would
never be served at all. That's why it is preferred.
Calls to both acquireRead(Object, long)
and
acquireWrite(Object, long)
are blocking and reentrant. Blocking
means they will wait if they can not acquire the descired access, reentrant means that a lock
request by a specific owner will always be compatible with other accesses on this lock by the
same owner. E.g. if you already have a lock for writing and you try to acquire write access
again you will not be blocked by this first lock, while others of course will be. This is the
natural way you already know from Java monitors and synchronized blocks.
- Version:
- $Revision$
- See Also:
GenericLock
Method Summary |
boolean |
acquireRead(java.lang.Object ownerId,
long timeoutMSecs)
Tries to acquire a blocking, reentrant read lock. |
boolean |
acquireWrite(java.lang.Object ownerId,
long timeoutMSecs)
Tries to acquire a blocking, reentrant write lock. |
Methods inherited from class org.apache.commons.transaction.locking.GenericLock |
acquire, acquire, acquire, acquire, equals, getConflictingOwners, getConflictingOwners, getConflictingWaiters, getLevelMaxLock, getLevelMinLock, getLockLevel, getMaxLevelOwner, getMaxLevelOwner, getMaxLevelOwner, getMaxLevelOwner, getOwner, getResourceId, has, hashCode, isCompatible, registerWaiter, release, setLockLevel, test, toString, tryLock, tryLock, unregisterWaiter |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
NO_LOCK
public static final int NO_LOCK
- See Also:
- Constant Field Values
READ_LOCK
public static final int READ_LOCK
- See Also:
- Constant Field Values
WRITE_LOCK
public static final int WRITE_LOCK
- See Also:
- Constant Field Values
ReadWriteLock
public ReadWriteLock(java.lang.Object resourceId,
LoggerFacade logger)
- Creates a new read/write lock.
- Parameters:
resourceId
- identifier for the resource associated to this locklogger
- generic logger used for all kind of debug logging
acquireRead
public boolean acquireRead(java.lang.Object ownerId,
long timeoutMSecs)
throws java.lang.InterruptedException
- Tries to acquire a blocking, reentrant read lock. A read lock is
compatible with other read locks, but not with a write lock.
- Parameters:
ownerId
- a unique id identifying the entity that wants to acquire a
certain lock level on this locktimeoutMSecs
- if blocking is enabled by the wait
parameter
this specifies the maximum wait time in milliseconds
- Returns:
true
if the lock actually was acquired
- Throws:
java.lang.InterruptedException
- when the thread waiting on this method is interrupted
acquireWrite
public boolean acquireWrite(java.lang.Object ownerId,
long timeoutMSecs)
throws java.lang.InterruptedException
- Tries to acquire a blocking, reentrant write lock. A write lock is
incompatible with any another read or write lock and is thus exclusive.
- Parameters:
ownerId
- a unique id identifying the entity that wants to acquire a
certain lock level on this locktimeoutMSecs
- if blocking is enabled by the wait
parameter
this specifies the maximum wait time in milliseconds
- Returns:
true
if the lock actually was acquired
- Throws:
java.lang.InterruptedException
- when the thread waiting on this method is interrupted
Copyright ? 2004 The Apache Software Foundation. All Rights Reserved.