Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,67 @@

public class BoundedBlockingQueue<T> {

private final Object[] array;
int capacity;
int tail;
int head;
int fill;


public BoundedBlockingQueue(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException();
}
this.array = new Object[capacity];
this.capacity = capacity;

this.head = 0;
this.tail = 0;
this.fill = 0;
}

public void put(T item) {
public void put(T item) throws InterruptedException {
if (item == null) {
throw new NullPointerException();
}
synchronized (this) {
while (fill == capacity) {
this.wait();
}
array[tail] = item;
tail = (tail + 1) % capacity;
fill++;
this.notifyAll();

}

}

public T take() {
return null;
public T take() throws InterruptedException {
synchronized (this) {
while (fill == 0) {
wait();
}

T item = (T) array[head];
array[head] = null;
head = (head + 1) % capacity;
fill--;

this.notifyAll();
return item;

}


}

public int size() {
return 0;
return fill;
}

public int capacity() {
return 0;

return capacity;
}
}
Loading