Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## 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`

Nested Class Summary

Nested classes inherited from class org.apache.commons.transaction.locking.GenericLock
`GenericLock.LockOwner`

Field Summary
`static int` | `NO_LOCK`
`static int` | `READ_LOCK`
`static int` | `WRITE_LOCK`

Fields inherited from class org.apache.commons.transaction.locking.GenericLock
`logger, owners, resourceId, waiters, waitingOwners`

Fields inherited from interface org.apache.commons.transaction.locking.MultiLevelLock2
`COMPATIBILITY_NONE, COMPATIBILITY_REENTRANT, COMPATIBILITY_REENTRANT_AND_SUPPORT, COMPATIBILITY_SUPPORT`

Constructor Summary
`ReadWriteLock(Object resourceId, LoggerFacade logger)` Creates a new read/write lock.

Method Summary
`boolean` | `acquireRead(Object ownerId, long timeoutMSecs)` Tries to acquire a blocking, reentrant read lock.
`boolean` | `acquireWrite(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`

Field Detail

### 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

Constructor Detail

### ReadWriteLock

    public ReadWriteLock(Object resourceId,
                         LoggerFacade logger)

  Creates a new read/write lock.

Parameters:
  `resourceId` - identifier for the resource associated to this lock
  `logger` - generic logger used for all kind of debug logging

Method Detail

### acquireRead

    public boolean acquireRead(Object ownerId,
                               long timeoutMSecs)
                        throws 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 lock
    `timeoutMSecs` - 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:
    `InterruptedException` - when the thread waiting on this method is interrupted

### acquireWrite

    public boolean acquireWrite(Object ownerId,
                                long timeoutMSecs)
                         throws 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 lock
    `timeoutMSecs` - 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:
    `InterruptedException` - when the thread waiting on this method is interrupted

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2004 The Apache Software Foundation. All Rights Reserved.