From 37de3bbf8b3cc45eb4b8e5599727e5cd9cfd026c Mon Sep 17 00:00:00 2001 From: VictoriaGrudtsyna Date: Mon, 16 Mar 2026 21:04:08 +0300 Subject: [PATCH] queue: all methods are implemented --- .../tasks/queue/BoundedBlockingQueue.java | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 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..2a1bcf4 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,53 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; +import java.util.Queue; + public class BoundedBlockingQueue { + private int capacity; + private Queue queue; + private int size; public BoundedBlockingQueue(int capacity) { - + if (capacity > 0) { + this.queue = new LinkedList<>(); + this.capacity = capacity; + } + else { + throw new IllegalArgumentException("capacity should be positive!"); + } } public void put(T item) throws InterruptedException { - + if (item == null) { + throw new IllegalArgumentException("item should be not null!"); + } + synchronized(queue) { + while (queue.size() >= capacity) { + queue.wait(); + } + queue.add(item); + queue.notifyAll(); + } } public T take() throws InterruptedException { - return null; + synchronized (queue) { + while (queue.size() == 0) { + queue.wait(); + } + T takenElem = queue.poll(); + queue.notifyAll(); + return takenElem; + } } - public int size() { - return 0; + public synchronized int size() { + return queue.size(); } public int capacity() { - return 0; + return capacity; } }