edu.emory.mathcs.util.remote.locks
Class ReentrantDistributedLock

java.lang.Object
  extended byedu.emory.mathcs.util.remote.locks.ReentrantDistributedLock
All Implemented Interfaces:
java.rmi.Remote, RemoteLock

public class ReentrantDistributedLock
extends java.lang.Object
implements RemoteLock

Implements a reentrant lock on top of a possibly non-reentrant remote lock so that local threads are scheduled within the JVM instead of competing for a remote resource. Depends on a correctly implemented semantics of equals for the remote lock (see RemoteLock).

Author:
Dirk Gorissen , Dawid Kurzyniec

Constructor Summary
ReentrantDistributedLock(RemoteLock remoteLock)
          Create a new ReentrantRemoteLock instance.
 
Method Summary
 boolean equals(java.lang.Object other)
           
 int hashCode()
           
 void lock()
          Acquires the remote lock. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.
 void lockInterruptibly()
          Acquires the remote lock unless the current thread is interrupted. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
  • The lock is acquired by the current thread; or
  • Some other thread interrupts the current thread, and interruption of lock acquisition is supported.
 RemoteCondition newCondition()
          Returns a new Condition instance that is bound to this RemoteLock instance.
 boolean tryLock()
          Acquires the remote lock only if it is free at the time of invocation. If the lock is not available then this method will return immediately with the value false.
 boolean tryLock(long timeout, TimeUnit unit)
          Acquires the remote lock if it is free within the given waiting time and the current thread has not been interrupted. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
  • The lock is acquired by the current thread; or
  • Some other thread interrupts the current thread, and interruption of lock acquisition is supported; or
  • The specified waiting time elapses
 void unlock()
          Releases the remote lock.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReentrantDistributedLock

public ReentrantDistributedLock(RemoteLock remoteLock)
Create a new ReentrantRemoteLock instance.

Method Detail

lock

public void lock()
          throws java.rmi.RemoteException
Acquires the remote lock. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

Specified by:
lock in interface RemoteLock
Throws:
java.rmi.RemoteException - if a communication error occurs

tryLock

public boolean tryLock()
                throws java.rmi.RemoteException
Acquires the remote lock only if it is free at the time of invocation. If the lock is not available then this method will return immediately with the value false.

Specified by:
tryLock in interface RemoteLock
Returns:
true if the lock was acquired and false otherwise.
Throws:
java.rmi.RemoteException - if a communication error occurs

lockInterruptibly

public void lockInterruptibly()
                       throws java.lang.InterruptedException,
                              java.rmi.RemoteException
Acquires the remote lock unless the current thread is interrupted. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

Specified by:
lockInterruptibly in interface RemoteLock
Throws:
java.rmi.RemoteException - if a communication error occurs
java.lang.InterruptedException - if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported).

tryLock

public boolean tryLock(long timeout,
                       TimeUnit unit)
                throws java.lang.InterruptedException,
                       java.rmi.RemoteException
Acquires the remote lock if it is free within the given waiting time and the current thread has not been interrupted. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

Specified by:
tryLock in interface RemoteLock
Parameters:
timeout - the maximum time to wait for the lock
unit - the time unit of the time argument.
Returns:
true if the lock was acquired and false if the waiting time elapsed before the lock was acquired.
Throws:
java.rmi.RemoteException - if a communication error occurs
java.lang.InterruptedException - if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported).

unlock

public void unlock()
            throws java.rmi.RemoteException
Releases the remote lock.

Specified by:
unlock in interface RemoteLock
Throws:
java.rmi.RemoteException - if a communication error occurs

newCondition

public RemoteCondition newCondition()
                             throws java.rmi.RemoteException
Description copied from interface: RemoteLock
Returns a new Condition instance that is bound to this RemoteLock instance. Before waiting on the condition the lock must be held by the current thread.

Specified by:
newCondition in interface RemoteLock
Returns:
A new Condition instance for this RemoteLock instance.
Throws:
java.rmi.RemoteException

hashCode

public int hashCode()

equals

public boolean equals(java.lang.Object other)