com.antiaction.critical
Class Mutex

java.lang.Object
  |
  +--com.antiaction.critical.Mutex

public class Mutex
extends java.lang.Object

Mutex semaphore, restricts access to one thread at a time. Based on an implementation by Scott Oaks & Henry Wong. Java Threads ISBN 1-56592-418-5.

Basicly this class allows for the maintenance of a so called critical area. Access is restricted to one thread at at time.

This should be a very robust implementation.

In depth coverage of the subject can be found in the afore mentioned book.

Version:
1.00
Author:
Nicholas Clarke

Constructor Summary
Mutex()
          Initialize open mutex semaphore.
Mutex(int cnt)
          Initialize mutex semaphore.with state.
 
Method Summary
 boolean attemptSemaphore()
          Attempt to obtain ownership of the lock, returns either way.
 java.lang.Thread getOwner()
          Returns the thread currently owning this lock.
 void obtainSemaphore()
          This call returns when the current thread has obtained ownership of the lock.
 void releaseSemaphore()
          Release ownership of the lock in case the nest count is zero.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Mutex

public Mutex()
Initialize open mutex semaphore.
See Also:
Mutex(int)

Mutex

public Mutex(int cnt)
Initialize mutex semaphore.with state. State depends on the argument.
Parameters:
cnt - initial nest count.
Throws:
java.lang.IllegalArgumentException - in case a negative argument is passed.
Method Detail

obtainSemaphore

public void obtainSemaphore()
This call returns when the current thread has obtained ownership of the lock.
See Also:
attemptSemaphore(), releaseSemaphore()

attemptSemaphore

public boolean attemptSemaphore()
Attempt to obtain ownership of the lock, returns either way.
Returns:
boolean indicating whether the attempt was successful or not.
See Also:
obtainSemaphore()

releaseSemaphore

public void releaseSemaphore()
Release ownership of the lock in case the nest count is zero.
Throws:
IllegalStateException - in case an attempt is made to release a lock that is not owned.
See Also:
obtainSemaphore()

getOwner

public java.lang.Thread getOwner()
Returns the thread currently owning this lock.
Returns:
thread currently owning this lock.