| |
|
| java.lang.Object com.sun.media.jai.util.RWLock
RWLock | final public class RWLock (Code) | | A class that provides a classic reader/writer lock functionality.
This implementation is based on the JavaWorld article by Tark Modi
titled "Lock on to an alternate synchronized mechanism" with some
minor modifications.
This lock provides the following functionality :
- Allows multiple threads read access while only a single
thread can have a write access.
- Allows a thread owning a read lock to "upgrade" to a write
lock.
- Allows a thread owning a write lock to "downgrade" to a read
lock.
- Allows the option to either block or specify a waitTime on
certain requests.
|
Constructor Summary | |
public | RWLock(boolean allowUpgrades) Constructor. | public | RWLock() Constructor. |
Method Summary | |
public synchronized boolean | downgrade() Tries to downgrade a write lock to a read lock. | public synchronized boolean | forReading(int waitTime) Tries to obtain a read lock within the specified time.
Parameters: waitTime - the time to wait in milliseconds while tryingto obtain the read lock. | public synchronized boolean | forReading() Tries to obtain a read lock. | public synchronized boolean | forWriting(int waitTime) Tries to obtain a write lock withing the specified time. | public synchronized boolean | forWriting() Tries to obtain a write lock. | public synchronized void | release() Tries to relinquish the ownership of a lock. | public synchronized boolean | upgrade(int waitTime) Try to upgrade a write lock within the specified amount of time. | public synchronized boolean | upgrade() Tries to upgrade to a write lock. |
RWLock | public RWLock(boolean allowUpgrades)(Code) | | Constructor.
Parameters: should - upgrades of read locks to write locks beallowed ? |
RWLock | public RWLock()(Code) | | Constructor. Equivalent to RWLock(true)
which creates an upgradable reader-writer lock.
|
downgrade | public synchronized boolean downgrade() throws LockNotHeld(Code) | | Tries to downgrade a write lock to a read lock. If the current
thread does not hold the lock an exception is thrown. If the
current thread already owns a read lock, nothing happens. If it
owns a write lock, then it is downgraded to a read lock. All
threads waiting for a read lock can now get it only if there are
no other threads waiting for a write lock ahead of them.
true if the downgrade was performed successfully. throws: LockNotHeld - if the current thread does not own the lock. |
forReading | public synchronized boolean forReading(int waitTime)(Code) | | Tries to obtain a read lock within the specified time.
Parameters: waitTime - the time to wait in milliseconds while tryingto obtain the read lock. A negative value indicates ablocking wait. true if the read lock was obtained without timing out. |
forReading | public synchronized boolean forReading()(Code) | | Tries to obtain a read lock. Equivalent to forReading(-1)
which will go into a blocking wait attempting to get a
read lock.
true, always. |
forWriting | public synchronized boolean forWriting(int waitTime) throws UpgradeNotAllowed(Code) | | Tries to obtain a write lock withing the specified time. If the
current thread owns a read lock, then it is upgraded to a write
lock if upgrades are allowed, else, throws an UpgradeNotAllowed
exception. If the lock is not owned by the current thread then
it waits for a write lock for the specified time.
Parameters: waitTime - the time to wait in milliseconds while tryingto obtain the write lock. A negative value indicates ablocking wait. true if the write lock was obtained without timing out. throws: UpgradeNotAllowed - if current thread owns a read lockand upgrades are not allowed. |
forWriting | public synchronized boolean forWriting() throws UpgradeNotAllowed(Code) | | Tries to obtain a write lock. Equivalent to forWriting(-1)
which will go into a blocking wait attempting to get a
write lock.
true, always. throws: UpgradeNotAllowed - if current thread owns a read lockand upgrades are not allowed. |
release | public synchronized void release() throws LockNotHeld(Code) | | Tries to relinquish the ownership of a lock. If the
current thread does not hold the lock an exception is
thrown. Note that every call to forReading
and forWriting must have a corresponding
release() call. However, upgrade()
and downgrade() do not have corresponding
release() calls.
throws: LockNotHeld - if the current thread does not own the lock. |
upgrade | public synchronized boolean upgrade(int waitTime) throws UpgradeNotAllowed, LockNotHeld(Code) | | Try to upgrade a write lock within the specified amount of time.
If the current thread does not own the lock or if upgrades
are not allowed an exception is thrown. If the current thread
already owns a write lock, nothing happens. Otherwise, threads
already owning a read lock are given a higher priority in
receiving the write lock than those that own no lock.
Parameters: waitTime - the time to wait in milliseconds while tryingto upgrade to a write lock. A negative value indicates ablocking wait. true if the upgrade was performed without timing out. throws: LockNotHeld - if the current thread does not own the lock throws: UpgradeNotAllowed - if the lock is not currentlyheld by the owner. |
upgrade | public synchronized boolean upgrade() throws UpgradeNotAllowed, LockNotHeld(Code) | | Tries to upgrade to a write lock. Equivalent to
upgrade(-1) which will go into a blocking wait
attempting to get a upgrade to a write lock.
true, always. throws: LockNotHeld - if some other thread owns the lock throws: UpgradeNotAllowed - if the lock is not currentlyheld by the owner. |
|
|
|