From 916f06df90c209b56a45d5fd429c4cb6185e4bab Mon Sep 17 00:00:00 2001 From: Milanya Date: Sat, 21 Mar 2026 01:26:25 +0300 Subject: [PATCH] queue: first try --- .../tasks/queue/BoundedBlockingQueue.java | 41 ++++++++++++++++--- 1 file changed, 35 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..3a3920a 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,54 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; +import java.util.Queue; + public class BoundedBlockingQueue { + private final Queue q; + private final int capacity; + private int size; public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException(); + } + this.capacity = capacity; + q = new LinkedList(); + size = 0; } public void put(T item) throws InterruptedException { - + if (item == null) { + throw new NullPointerException(); + } + synchronized (this) { + while (size == capacity) { + wait(); + } + q.add(item); + size++; + notifyAll(); + } } public T take() throws InterruptedException { - return null; + synchronized (this) { + while (size == 0) { + wait(); + } + T item = q.poll(); + size--; + notifyAll(); + return item; + } } - public int size() { - return 0; + public synchronized int size() { + return size; } public int capacity() { - return 0; + return capacity; } }