goodsjpi
Class Mutex

java.lang.Object
  extended bygoodsjpi.Mutex

public class Mutex
extends java.lang.Object

Class for providing mutual exclusion of threads. Only one thread can enter critical section guarded by mutex (but can do it several times).


Field Summary
protected  int nBlocked
           
protected  int nested
           
protected  java.lang.Thread owner
           
 
Constructor Summary
Mutex()
           
 
Method Summary
 void enter()
          Lock mutex.
 boolean enter(long timeout)
          Try to lock mutex within specified period of time.
 void leave()
          Release mutex.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

owner

protected java.lang.Thread owner

nested

protected int nested

nBlocked

protected int nBlocked
Constructor Detail

Mutex

public Mutex()
Method Detail

enter

public void enter()
Lock mutex. This method should be called before entering critical section.


enter

public boolean enter(long timeout)
Try to lock mutex within specified period of time. This method should be called before entering critical section.

Parameters:
timeout - the maximum time to wait in milliseconds.
Returns:
true if mutex is successfully locked, false if enter() was terminated due to timeout expiration.

leave

public void leave()
Release mutex. This method should be called after exit from critical section. Mutex will be unlocked only if number of leave() invocations is equal to the number of enter() invocations.