From e89d4bef04b0eaaaaa94134ead014ad0152ab942 Mon Sep 17 00:00:00 2001 From: siwreienta Date: Wed, 18 Mar 2026 02:12:00 +0300 Subject: [PATCH] queue: first try --- .../tasks/queue/BoundedBlockingQueue.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 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..8450809 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,50 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.ArrayDeque; +import java.util.Deque; + public class BoundedBlockingQueue { + private final int capacity_; + private final Deque data_; public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException("`constructor` argument error: capacity must be positive"); + } + this.capacity_ = capacity; + this.data_ = new ArrayDeque<>(capacity); } public void put(T item) throws InterruptedException { - + if (item == null) { + throw new NullPointerException("`put` argument error: item cannot be null"); + } + synchronized (this) { + while (data_.size() == capacity_) { + wait(); + } + data_.addLast(item); + notifyAll(); + } } public T take() throws InterruptedException { - return null; + synchronized (this) { + while (data_.isEmpty()) { + wait(); + } + T value = data_.removeFirst(); + notifyAll(); + return value; + } } public int size() { - return 0; + return data_.size(); } public int capacity() { - return 0; + return capacity_; } }