From 7a1f7c3871ea1def8a3086c520e824b4e212c815 Mon Sep 17 00:00:00 2001 From: Andrey Chertykovtsev Date: Sun, 5 Apr 2026 21:03:51 +0200 Subject: [PATCH] Implement thread-safe `put` and `take` methods in `BlockingQueue`. --- src/main/java/core/basesyntax/BlockingQueue.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/BlockingQueue.java b/src/main/java/core/basesyntax/BlockingQueue.java index 77a20440..f993ee58 100644 --- a/src/main/java/core/basesyntax/BlockingQueue.java +++ b/src/main/java/core/basesyntax/BlockingQueue.java @@ -12,16 +12,22 @@ public BlockingQueue(int 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 (queue.isEmpty()) { + wait(); + } + notifyAll(); + return queue.remove(); } public synchronized boolean isEmpty() { - // write your code here - return true; + return queue.isEmpty(); } }