com.triactive.jdo.util
Class ReadWriteLock

java.lang.Object
  extended bycom.triactive.jdo.util.ReadWriteLock

public class ReadWriteLock
extends java.lang.Object

A simple read-write lock implementation. Multiple threads may lock using readLock(), only one can lock using writeLock(). The caller is responsible for coding a try-finally that ensures unlock() is called for every readLock() and writeLock() call.

A ReadWriteLock is recursive; with one exception, a thread can re-lock an object it already has locked. Multiple read locks can be acquired by the same thread, as can multiple write locks. The exception however is that a write lock cannot be acquired when a read lock is already held (to allow this would cause deadlocks).

Successive lock calls from the same thread must be matched by an equal number of unlock() calls.

Author:
Mike Martin

Constructor Summary
ReadWriteLock()
          Constructs read-write lock.
 
Method Summary
 void readLock()
          Acquire a read lock.
 java.lang.String toString()
           
 void unlock()
          Release a read or write lock.
 void writeLock()
          Acquire a write lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReadWriteLock

public ReadWriteLock()
Constructs read-write lock.

Method Detail

readLock

public void readLock()
              throws java.lang.InterruptedException
Acquire a read lock. The calling thread will be suspended until no other thread holds a write lock.

If the calling thread already owns a write lock for the object a read lock is immediately acquired.

Throws:
java.lang.InterruptedException - If the thread is interrupted while attempting to acquire the lock.

writeLock

public void writeLock()
               throws java.lang.InterruptedException
Acquire a write lock. The calling thread will be suspended until no other thread holds a read or write lock.

This method cannot be called if the thread already owns a read lock on the same ReadWriteLock object, otherwise an IllegalStateException is thrown.

Throws:
java.lang.IllegalStateException - If the thread already holds a read lock on the same object.
java.lang.InterruptedException - If the thread is interrupted while attempting to acquire the lock.

unlock

public void unlock()
Release a read or write lock. Must be called in a finally block after acquiring a lock.


toString

public java.lang.String toString()


Copyright 2001-2007 The TJDO Project All Rights Reserved.