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..c335f5fd 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,51 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.LinkedList; +public class BoundedBlockingQueue { + private final LinkedList list = new LinkedList<>(); + private int capacity; public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException("capacity must be positive"); + } + this.capacity = capacity; } - public void put(T item) { - + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException("item must be non-null"); + } + + synchronized (this) { + while (list.size() == capacity) { + this.wait(); + } + list.addLast(item); + notifyAll(); + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (this) { + while (list.isEmpty()) { + this.wait(); + } + T item = list.getFirst(); + list.removeFirst(); + notifyAll(); + return item; + } } public int size() { - return 0; + synchronized (this) { + return list.size(); + } } public int capacity() { - return 0; + return this.capacity; } }