diff --git a/src/main/java/core/basesyntax/BlockingQueue.java b/src/main/java/core/basesyntax/BlockingQueue.java index 77a20440..baf32fc7 100644 --- a/src/main/java/core/basesyntax/BlockingQueue.java +++ b/src/main/java/core/basesyntax/BlockingQueue.java @@ -4,24 +4,34 @@ import java.util.Queue; public class BlockingQueue { - private Queue queue = new LinkedList<>(); - private int capacity; + private final Queue queue = new LinkedList<>(); + private final int capacity; public BlockingQueue(int capacity) { this.capacity = capacity; } public synchronized void put(T element) throws InterruptedException { - // write your code here + while (queue.size() == capacity) { + wait(); + } + queue.add(element); + notifyAll(); } public synchronized T take() throws InterruptedException { - // write your code here - return null; + while (isEmpty()) { + wait(); + } + T value = queue.poll(); + notifyAll(); + return value; } public synchronized boolean isEmpty() { // write your code here - return true; + boolean isEmpty = queue.isEmpty(); + notifyAll(); + return isEmpty; } }