Skip to content
Open
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,41 @@

public class BoundedBlockingQueue<T> {

private final Object[] buffer;
private final int capacity;
private int head, tail, size;

public BoundedBlockingQueue(int capacity) {

if (capacity <= 0) throw new IllegalArgumentException("capacity must be > 0");
this.capacity = capacity;
this.buffer = new Object[capacity];
}

public void put(T item) throws InterruptedException {

public synchronized void put(T item) throws InterruptedException {
if (item == null) throw new NullPointerException("null items are not allowed");
while (size == capacity) wait();
buffer[tail] = item;
tail = (tail + 1) % capacity;
size++;
notifyAll();
}

public T take() throws InterruptedException {
return null;
@SuppressWarnings("unchecked")
public synchronized T take() throws InterruptedException {
while (size == 0) wait();
T item = (T) buffer[head];
buffer[head] = null;
head = (head + 1) % capacity;
size--;
notifyAll();
return item;
}

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

public int capacity() {
return 0;
return capacity;
}
}
Loading