From d3127e13ee921b051dcaed61397ee9124e2a89cc Mon Sep 17 00:00:00 2001 From: Timofeev_V_S Date: Sun, 22 Mar 2026 21:12:17 +0300 Subject: [PATCH] queue: --- .../tasks/queue/BoundedBlockingQueue.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) 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 816f3ee..e6bdcdd 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,49 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.LinkedList; +import java.util.Queue; +public class BoundedBlockingQueue { + private final Queue queue = new LinkedList<>(); + private final int capacity; public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException("Capacity must be positive"); + } + this.capacity = capacity; } public void put(T item) throws InterruptedException { - + if (item == null) { + throw new NullPointerException("Cannot put null into queue"); + } + + synchronized (queue) { + while (queue.size() == capacity) { + queue.wait(); + } + queue.add(item); + queue.notifyAll(); + } } public T take() throws InterruptedException { - return null; + synchronized (queue) { + while (queue.isEmpty()) { + queue.wait(); + } + T item = queue.poll(); + queue.notifyAll(); + return item; + } } - public int size() { - return 0; + public synchronized int size() { + return queue.size(); } public int capacity() { - return 0; + return capacity; } -} +} \ No newline at end of file