com.knowgate.multithreating
Class Semaphore

java.lang.Object
  extended by com.knowgate.multithreating.Semaphore

public final class Semaphore
extends java.lang.Object

Classical Dijkstra semaphore with wait() and signal() operations.


Field Summary
protected  int counter
           
 
Constructor Summary
Semaphore()
          Create semaphore with zero counter value.
Semaphore(int initValue)
          Create semaphore with specified non-negative counter value.
 
Method Summary
 void signal()
          Increment value of the counter.
 void waitSemaphore()
          Wait for non-zero value of counter.
 boolean waitSemaphore(long timeout)
          Wait at most timeout miliseconds for non-zero value of counter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

counter

protected int counter
Constructor Detail

Semaphore

public Semaphore()
Create semaphore with zero counter value.


Semaphore

public Semaphore(int initValue)
Create semaphore with specified non-negative counter value.

Parameters:
initValue - initial value of semaphore counter
Method Detail

waitSemaphore

public void waitSemaphore()
                   throws java.lang.InterruptedException
Wait for non-zero value of counter.

Throws:
java.lang.InterruptedException

waitSemaphore

public boolean waitSemaphore(long timeout)
                      throws java.lang.InterruptedException
Wait at most timeout miliseconds for non-zero value of counter.

Parameters:
timeout - the maximum time to wait in milliseconds.
Returns:
true if counter is not zero, false if wait() was terminated due to timeout expiration.
Throws:
java.lang.InterruptedException

signal

public void signal()
Increment value of the counter. If there are waiting threads, exactly one of them will be awaken.