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..844d2dee 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,59 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; + public class BoundedBlockingQueue { + final Object monitor; + int capacity; + LinkedList queue; + int curr_len; - public BoundedBlockingQueue(int capacity) { + public BoundedBlockingQueue(int capacity_) throws IllegalArgumentException { - } + if (capacity_ <= 0) { + throw new IllegalArgumentException(); + } - public void put(T item) { + monitor = new Object(); + capacity = capacity_; + queue = new LinkedList<>(); + curr_len = 0; + } + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException(); + } + synchronized (monitor) { + while (curr_len >= capacity) { + monitor.wait(); + } + queue.offer(item); + curr_len++; + monitor.notifyAll(); + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (monitor) { + while (curr_len <= 0) { + monitor.wait(); + } + curr_len--; + T item = queue.poll(); + monitor.notifyAll(); + return item; + } } public int size() { - return 0; + synchronized (monitor) { + return curr_len; + } } public int capacity() { - return 0; + return capacity; } }