M
metrix007
I have a Semaphore and Buffer class, but I am not sure if I have
implemented it correctly, it is posted here now, I was just after tips
and suggestions..or pointing out erros which are not obvious(to me).
/*
This class implements a bounded buffer that stores Objects and uses
blocking primitives based on semaphores when the buffer is full or
empty. The buffer can be accessed by any number of threads.
*/
public class Buffer
{
// ...
// private Object store[] = new Object[size];
private int inptr = 0;
private int outptr = 0;
private int size = 0;
Semaphore spaces = new Semaphore(size);
Semaphore elements = new Semaphore(0);
// creates a bounded buffer with capacity cap
public Buffer()
{
}
// places the object o in the buffer; if no space is left in
// the buffer, block until a space becomes available
public void put(Object o)
{
spaces.down() ;
// store[inptr] = o ;
inptr = (inptr+1) % size ;
elements.up() ;
}
// take an item from the buffer and return it; if there is no item in
// the buffer, block until one becomes available
public Object get()
{
Object value = null ;
elements.down() ;
// value = store[outptr] ;
outptr = (outptr+1) % size ;
spaces.up() ;
return value ;
}
}
and semaphore is pretty straighforward...
/*
This class implements a general semaphore. Since the semaphore
is blocked using Java condition synchronisation, no guarantee can
be made about the order in which blocked threads are woken up.
Most of this code is based on the semaphore implementation
provided in Magee & Kramer (pg 91).
*/
public class Semaphore
{
protected int value; // value of the semaphore
// class constructor that initialises the value of the semaphore
// to initial
public Semaphore(int initial)
{
value = initial;
}
// increment the value of the semaphore and wake up one waiting
// thread, if there is one
public synchronized void up()
{
++value;
notify();
}
// wait for the semaphore to have a positive value and then
// decrement the value by one
public synchronized void down()
{
// block thread until value of the semaphore is positive
while(value == 0) {
try {
wait();
} catch (java.lang.InterruptedException e) {
// if call is interrupted, print the current
// call stack, but do not exit the loop
e.printStackTrace(System.err);
}
}
// decrement value of the semaphore
--value;
}
}
many thanks
larry
implemented it correctly, it is posted here now, I was just after tips
and suggestions..or pointing out erros which are not obvious(to me).
/*
This class implements a bounded buffer that stores Objects and uses
blocking primitives based on semaphores when the buffer is full or
empty. The buffer can be accessed by any number of threads.
*/
public class Buffer
{
// ...
// private Object store[] = new Object[size];
private int inptr = 0;
private int outptr = 0;
private int size = 0;
Semaphore spaces = new Semaphore(size);
Semaphore elements = new Semaphore(0);
// creates a bounded buffer with capacity cap
public Buffer()
{
}
// places the object o in the buffer; if no space is left in
// the buffer, block until a space becomes available
public void put(Object o)
{
spaces.down() ;
// store[inptr] = o ;
inptr = (inptr+1) % size ;
elements.up() ;
}
// take an item from the buffer and return it; if there is no item in
// the buffer, block until one becomes available
public Object get()
{
Object value = null ;
elements.down() ;
// value = store[outptr] ;
outptr = (outptr+1) % size ;
spaces.up() ;
return value ;
}
}
and semaphore is pretty straighforward...
/*
This class implements a general semaphore. Since the semaphore
is blocked using Java condition synchronisation, no guarantee can
be made about the order in which blocked threads are woken up.
Most of this code is based on the semaphore implementation
provided in Magee & Kramer (pg 91).
*/
public class Semaphore
{
protected int value; // value of the semaphore
// class constructor that initialises the value of the semaphore
// to initial
public Semaphore(int initial)
{
value = initial;
}
// increment the value of the semaphore and wake up one waiting
// thread, if there is one
public synchronized void up()
{
++value;
notify();
}
// wait for the semaphore to have a positive value and then
// decrement the value by one
public synchronized void down()
{
// block thread until value of the semaphore is positive
while(value == 0) {
try {
wait();
} catch (java.lang.InterruptedException e) {
// if call is interrupted, print the current
// call stack, but do not exit the loop
e.printStackTrace(System.err);
}
}
// decrement value of the semaphore
--value;
}
}
many thanks
larry