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..e9cddcb7 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 @@ -1,25 +1,49 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.ArrayDeque; +import java.util.Queue; +public class BoundedBlockingQueue { + + private final Queue queue = new ArrayDeque<>(); + private final int capacity; public BoundedBlockingQueue(int capacity) { - + if(capacity <= 0){ + throw new IllegalArgumentException("Capacity <= 0 :^("); + } + this.capacity = capacity; } - public void put(T item) { + public synchronized void put(T item) throws InterruptedException{ + if(item == null){ + throw new NullPointerException("not null pls"); + } + while(queue.size() == capacity()){ + wait(); + } + + queue.add(item); + notifyAll(); } - public T take() { - return null; + public synchronized T take() throws InterruptedException{ + while(queue.isEmpty()){ + wait(); + } + + T item = queue.remove(); + notifyAll(); + + return item; } - public int size() { - return 0; + public synchronized int size() { + return queue.size(); } public int capacity() { - return 0; + return capacity; } -} +} \ No newline at end of file