Done
This commit is contained in:
parent
58b079621f
commit
82db24493f
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user