Class Semaphore

java.lang.Object
  |
  +--Semaphore

public class Semaphore
extends java.lang.Object

A Semaphore is used for synchronization of Java threads. A Semaphore has an integer value. A thread can increment the value by calling up(). A thread can decrement the value by calling down(). A thread that invokes down() waits until the value is positive before decrementing the value. A semaphore has a name which is determined by the string argument of its constructor.

A semaphore can be created for granting permission by invoking the constructor with an argument of 0. If thread A needs to be held up until thread B is ready then A invokes down() and B invokes up() when it is ready(). This increments the semaphore value, allowing A to proceed.

A semaphore can also be created for enforcing mutual exclusion by invoking the constructor with an argument of 1. Threads call down() when entering a critical code section and up() when leaving. This ensures that only one thread can be executing critical section code at a time.

Finally, a semaphore can be used for resource counting. For this use, the semaphore is created by calling the constructor with an argument equal to the initial number of instances of the counted resource. A thread invokes down() when acquiring an instance of the resource and invokes up() when releasing an instance.


Field Summary
protected  java.lang.String name
          name is the name for this semaphore.
protected static int numWaiters
          numWaiters is the number of threads that are waiting on semaphores.
protected  int value
          value is the current value of this semaphore.
 
Constructor Summary
Semaphore(java.lang.String nm, int init)
          new Semaphore(init, nm) returns a new semaphore object named nm with initial value init.
 
Method Summary
 void down()
          s.down() waits until the value of s is positive, then decrements the value of s.
 java.lang.String toString()
          s.toString() returns the name of s.
 void up()
          s.up() increments the value of s.
static int waiters()
          Semaphore.waiters() returns the number of threads that are waiting on semaphores.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

value

protected int value
value is the current value of this semaphore.

name

protected java.lang.String name
name is the name for this semaphore.

numWaiters

protected static int numWaiters
numWaiters is the number of threads that are waiting on semaphores.
Constructor Detail

Semaphore

public Semaphore(java.lang.String nm,
                 int init)
new Semaphore(init, nm) returns a new semaphore object named nm with initial value init.
Method Detail

up

public void up()
s.up() increments the value of s.

down

public void down()
s.down() waits until the value of s is positive, then decrements the value of s.

toString

public java.lang.String toString()
s.toString() returns the name of s.
Overrides:
toString in class java.lang.Object

waiters

public static int waiters()
Semaphore.waiters() returns the number of threads that are waiting on semaphores.