From ca4a7316a51fb33dad3fd9ff95ceb38773b19597 Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Tue, 26 May 2026 12:36:12 +0200 Subject: [PATCH 1/2] solution --- .../java/core/basesyntax/BlockingQueue.java | 25 +++++++++++-------- .../java/core/basesyntax/thread/Consumer.java | 4 +-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/core/basesyntax/BlockingQueue.java b/src/main/java/core/basesyntax/BlockingQueue.java index 77a20440..014f8177 100644 --- a/src/main/java/core/basesyntax/BlockingQueue.java +++ b/src/main/java/core/basesyntax/BlockingQueue.java @@ -4,24 +4,29 @@ 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 + public synchronized void put(T value) throws InterruptedException { + while (queue.size() == capacity) { + wait(); + } + + queue.add(value); + notifyAll(); } public synchronized T take() throws InterruptedException { - // write your code here - return null; - } + while (queue.isEmpty()) { + wait(); + } - public synchronized boolean isEmpty() { - // write your code here - return true; + T value = queue.poll(); + notifyAll(); + return value; } } diff --git a/src/main/java/core/basesyntax/thread/Consumer.java b/src/main/java/core/basesyntax/thread/Consumer.java index a28994fa..87682c28 100644 --- a/src/main/java/core/basesyntax/thread/Consumer.java +++ b/src/main/java/core/basesyntax/thread/Consumer.java @@ -3,7 +3,7 @@ import core.basesyntax.BlockingQueue; public class Consumer implements Runnable { - private BlockingQueue blockingQueue; + private final BlockingQueue blockingQueue; public Consumer(BlockingQueue blockingQueue) { this.blockingQueue = blockingQueue; @@ -11,7 +11,7 @@ public Consumer(BlockingQueue blockingQueue) { @Override public void run() { - while (!blockingQueue.isEmpty()) { + while (true) { try { System.out.println("Took value " + blockingQueue.take()); } catch (InterruptedException e) { From f79346df33346d15d54f10fa3259f737e7062e56 Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Tue, 26 May 2026 12:39:10 +0200 Subject: [PATCH 2/2] solution --- src/main/java/core/basesyntax/BlockingQueue.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/BlockingQueue.java b/src/main/java/core/basesyntax/BlockingQueue.java index 014f8177..bd5b6b18 100644 --- a/src/main/java/core/basesyntax/BlockingQueue.java +++ b/src/main/java/core/basesyntax/BlockingQueue.java @@ -16,7 +16,7 @@ public synchronized void put(T value) throws InterruptedException { wait(); } - queue.add(value); + queue.offer(value); notifyAll(); } @@ -29,4 +29,8 @@ public synchronized T take() throws InterruptedException { notifyAll(); return value; } + + public synchronized boolean isEmpty() { + return queue.isEmpty(); + } }