From d1fc3b8e8a0906655121893fcf1942b6a1f74bd9 Mon Sep 17 00:00:00 2001 From: Sofa Gareeva Date: Sat, 21 Mar 2026 17:30:36 +0300 Subject: [PATCH] queue: try --- .../tasks/queue/BoundedBlockingQueue.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 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..b59dd39 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,45 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; + public class BoundedBlockingQueue { + private final LinkedList queue; + private final int capacity; public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException(); + } + this.capacity = capacity; + this.queue = new LinkedList<>(); } - public void put(T item) throws InterruptedException { - + public synchronized void put(T item) throws InterruptedException { + if (item == null) { + throw new IllegalArgumentException(); + } + while (queue.size() == capacity) { + wait(); + } + queue.addLast(item); + notifyAll(); } - public T take() throws InterruptedException { - return null; + public synchronized T take() throws InterruptedException { + while (queue.isEmpty()) { + wait(); + } + T elem = queue.removeFirst(); + notifyAll(); + return elem; } - public int size() { - return 0; + public synchronized int size() { + return queue.size(); } - public int capacity() { - return 0; + public synchronized int capacity() { + return capacity; } }