This commit is contained in:
ArchUSB 2020-03-18 17:20:09 +01:00
parent 58b079621f
commit 82db24493f

View File

@ -15,9 +15,32 @@ class BoundedBuffer[T](size: Int) extends AbstractBoundedBuffer[T](size) {
// You do not need to create those variables yourself!
// They are inherited from the AbstractBoundedBuffer class.
override def put(e: T): Unit = ???
override def put(e: T): Unit = synchronized{
while(isFull) wait()
buffer(head) = e
head = nextHeadIndex;
count = count + 1;
notifyAll()
}
override def take(): T = ???
override def take(): T = synchronized{
while(isEmpty) wait()
val e = buffer(tailIndex);
buffer.delete(tailIndex);
count = count - 1;
notifyAll()
e
}
def isEmpty : Boolean = count == 0;
def isFull : Boolean = count == size
def tailIndex : Int = {
val diff = head - count
if(diff >= 0) diff
else diff + size
}
def nextHeadIndex : Int = (head + 1) % size
// You may want to add methods to:
// - check whether the buffer is empty