com.groiss.ds
Class CountedSemaphore

java.lang.Object
  extended by com.groiss.ds.CountedSemaphore

public class CountedSemaphore
extends java.lang.Object

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.


Constructor Summary
CountedSemaphore(int threadsRunning)
          Construct a new semaphore.
CountedSemaphore(int threadsRunning, int threadsWaiting)
          Construct a new semaphore.
 
Method Summary
static void addSemaphore(java.lang.String id, CountedSemaphore s)
          Add a new semaphore to the semaphore table
 java.util.LinkedList getQueue()
          Returns the queue of waiting threads.
static CountedSemaphore getSemaphore(java.lang.String id)
          Get the semaphore with key 'id' from the semaphore table
 void P()
          Enter the critical section.
 java.lang.String toString()
           
 void V()
          Leave the critical section.
 int value()
          Returns the number of resources still available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 Detail

getSemaphore

public static CountedSemaphore getSemaphore(java.lang.String id)
Get the semaphore with key 'id' from the semaphore table


addSemaphore

public static void addSemaphore(java.lang.String id,
                                CountedSemaphore s)
Add a new semaphore to the semaphore table


P

public void P()
       throws QueueFullException
Enter the critical section.

Throws:
QueueFullException - when waiting queue is full.

V

public void V()
Leave the critical section.


value

public int value()
Returns the number of resources still available. Do not do a `if (s.value() > 0) s.P(); else ...' because there is a race condition.

Returns:
number of resources

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getQueue

public java.util.LinkedList getQueue()
Returns the queue of waiting threads. The queue contains Locker objects (not public) the toString() method of them return the threads name.

Returns:
the waiter queue


Copyright © 2001-2006 Groiss Informatics GmbH. All Rights Reserved.