From 73a83e5ae7389b0d2a8a102937d030855e02acbb Mon Sep 17 00:00:00 2001 From: hse_sadunaev Date: Wed, 18 Mar 2026 01:44:01 +0300 Subject: [PATCH] queue: lol --- .../tasks/queue/BoundedBlockingQueue.java | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 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..4ba15c0 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,42 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { - - - public BoundedBlockingQueue(int capacity) { - - } +import java.util.LinkedList; +import java.util.Queue; - public void put(T item) throws InterruptedException { - - } - - public T take() throws InterruptedException { - return null; - } - - public int size() { - return 0; - } +public class BoundedBlockingQueue { - public int capacity() { - return 0; - } + private final int capacity; + private Queue queue = new LinkedList(); + + public BoundedBlockingQueue(int capacity) { + if (capacity <= 0){ + throw new IllegalArgumentException(); + } + this.capacity = capacity; + } + + public synchronized void put(T item) throws InterruptedException { + if (item == null){ + throw new IllegalArgumentException(); + } + + while (queue.size() == capacity) wait(); + queue.offer(item); + notifyAll(); + } + + public synchronized T take() throws InterruptedException { + while(queue.isEmpty()) wait(); + T item = queue.poll(); + notifyAll(); + return item; + } + + public synchronized int size() { + return queue.size(); + } + + public synchronized int capacity() { + return capacity; + } }