From 5da226a021cca67089fc724c9321e0c7352c07e8 Mon Sep 17 00:00:00 2001 From: Yuliia Date: Thu, 5 Feb 2026 11:54:35 +0200 Subject: [PATCH] solved task --- .../java/core/basesyntax/BlockingQueue.java | 17 ++++++++++++----- .../java/core/basesyntax/thread/Consumer.java | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/BlockingQueue.java b/src/main/java/core/basesyntax/BlockingQueue.java index 77a20440..c28c6c7b 100644 --- a/src/main/java/core/basesyntax/BlockingQueue.java +++ b/src/main/java/core/basesyntax/BlockingQueue.java @@ -12,16 +12,23 @@ 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(); + } + T removed = queue.remove(); + notifyAll(); + return removed; } public synchronized boolean isEmpty() { - // write your code here - return true; + return queue.isEmpty(); } } diff --git a/src/main/java/core/basesyntax/thread/Consumer.java b/src/main/java/core/basesyntax/thread/Consumer.java index a28994fa..00ed1b52 100644 --- a/src/main/java/core/basesyntax/thread/Consumer.java +++ b/src/main/java/core/basesyntax/thread/Consumer.java @@ -11,7 +11,7 @@ public Consumer(BlockingQueue blockingQueue) { @Override public void run() { - while (!blockingQueue.isEmpty()) { + for (int i = 0; i < 50; i++) { try { System.out.println("Took value " + blockingQueue.take()); } catch (InterruptedException e) {