diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java index 5c686cff..6549806b 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java @@ -2,24 +2,43 @@ public class BoundedBlockingQueue { + 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; } }