Package com.groiss.ds
Class CountedSemaphore
java.lang.Object
java.util.concurrent.Semaphore
com.groiss.ds.CountedSemaphore
- All Implemented Interfaces:
Serializable
A CountedSemaphore (from Dijkstra) where waiting threads are handled in FIFO order.
Moreover you can specify the amount of threads waiting for the resource. If the
waiting queue is full the next caller will receive an QueueFullException.
- See Also:
-
Constructor Summary
ConstructorDescriptionCountedSemaphore
(int threadsRunning) Construct a new semaphore.CountedSemaphore
(int threadsRunning, int threadsWaiting) Construct a new semaphore. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addSemaphore
(String id, CountedSemaphore s) Add a new semaphore to the semaphore tablestatic CountedSemaphore
getSemaphore
(String id) Get the semaphore with key 'id' from the semaphore tablevoid
P()
Enter the critical section.void
V()
Leave the critical section.Methods inherited from class java.util.concurrent.Semaphore
acquire, acquire, acquireUninterruptibly, acquireUninterruptibly, availablePermits, drainPermits, getQueuedThreads, getQueueLength, hasQueuedThreads, isFair, reducePermits, release, release, toString, tryAcquire, tryAcquire, tryAcquire, tryAcquire
-
Constructor Details
-
CountedSemaphore
public CountedSemaphore(int threadsRunning) Construct a new semaphore.- Parameters:
threadsRunning
- how many threads can go into the critical section.
-
CountedSemaphore
public CountedSemaphore(int threadsRunning, int threadsWaiting) Construct a new semaphore.- Parameters:
threadsRunning
- how many threads can go into the critical section.threadsWaiting
- how many threads may wait.
-
-
Method Details
-
getSemaphore
Get the semaphore with key 'id' from the semaphore table -
addSemaphore
Add a new semaphore to the semaphore table -
P
Enter the critical section.- Throws:
QueueFullException
- when waiting queue is full.InterruptedException
-
V
public void V()Leave the critical section.
-