|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--it.pixel.util.threads.Semaphore
This class is the implementation of a semaphore.
The lock() and unlock() methods implement repectively what historically has been known as the WAIT() and SIGNAL() (or P() and V()) model.
A semaphore is a positive integer counter that can be modified only by one thread at a time, through its lock() and unlock() methods.
Each call to lock() waits for the counter value to become greater than zero. When it becomes greater than zero, or if it already is, lock() decreases by one the value of the counter and returns.
Each call to unlock() increases by one the value of the counter.
Java monitors can always be used instead of semaphores for the same pourposes, it's only a matter of taste.
Field Summary | |
protected int |
maxValue
The highest value this semaphore can grow. |
protected int |
value
The current value of this semaphore. |
Constructor Summary | |
Semaphore()
Same as Semaphore(0, Integer.MAX_VALUE) |
|
Semaphore(int initialValue)
Same as Semaphore(initialValue, Integer.MAX_VALUE) |
|
Semaphore(int initialValue,
int maxValue)
Creates a new semaphore with specified values. |
Method Summary | |
int |
getApproximateValue()
Returns the current value of the counter. |
void |
lock()
Waits for the counter to be positive, then decreases it by one and returns |
void |
unlock()
If the current value is less than the maximum value, it increases the counter by one, wakes up a thread waiting in the lock() method if there is one, and returns. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected int value
protected int maxValue
Constructor Detail |
public Semaphore(int initialValue, int maxValue)
initialValue
- The initial value (0 creates a "initially locked" semaphore).maxValue
- The maximum valuepublic Semaphore(int initialValue)
initialValue
- public Semaphore()
Method Detail |
public void lock()
public void unlock()
public int getApproximateValue()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |