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..ed8698ce 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,50 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; +import java.util.Queue; + public class BoundedBlockingQueue { + Queue queue; + int capacity; + int currentAmount; public BoundedBlockingQueue(int capacity) { - + if (capacity<=0){ + throw new IllegalArgumentException(); + } + this.capacity = capacity; + queue = new LinkedList<>(); + currentAmount = 0; } - public void put(T item) { - + public synchronized void put(T item) throws InterruptedException { + if (item == null){ + throw new NullPointerException(); + } + if (currentAmount==capacity){ + wait(); + } + queue.add(item); + currentAmount++; + notifyAll(); } - public T take() { - return null; + public synchronized T take() throws InterruptedException { + if (currentAmount==0){ + wait(); + } + T val = queue.poll(); + currentAmount--; + notifyAll(); + return val; } - public int size() { - return 0; + public synchronized int size() { + return currentAmount; } - public int capacity() { - return 0; + public synchronized int capacity() { + return capacity; } }