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,43 @@

public class BoundedBlockingQueue<T> {

int head, tail, count;
Object[] items;

public BoundedBlockingQueue(int capacity) {

if (capacity <= 0) {
throw new IllegalArgumentException();
}
items = new Object[capacity];
}

public void put(T item) {

public synchronized void put(T item) throws InterruptedException {
while (count == items.length) {
wait();
}
items[tail] = item;
tail = (tail + 1) % items.length;
++count;
notifyAll();
}

public T take() {
return null;
public synchronized T take() throws InterruptedException {
while (count == 0) {
wait();
}
T item = (T)items[head];
items[head] = null;
head = (head + 1) % items.length;
--count;
notifyAll();
return item;
}

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

public int capacity() {
return 0;
return items.length;
}
}
Loading