public class Semaphore
extends java.lang.Object
implements multi-slot semaphore. (This is somewhere available in the jdk and I just missed it, right?)
Constructor and Description |
---|
Semaphore(int slots)
creates a semaphore with the given number of slots.
|
Modifier and Type | Method and Description |
---|---|
int |
acquire()
blocks the calling thread until a free slot is available.
|
void |
release(int slot)
releases the given slot and thereby allows other threads to
acquire it.
|
public Semaphore(int slots)
creates a semaphore with the given number of slots.
public int acquire()
blocks the calling thread until a free slot is available. The
slot number as a value starting from zero is returned. It will
typically be used by the caller as and index into an array of
resources. Once the caller does not need the resource anymore, it
should call release()
to free the slot for
others.
public void release(int slot)
releases the given slot and thereby allows other threads to acquire it. No check is and can be made as to whether it is ok to release the slot. To assure this is completely the responsibility of the caller.