From f3a34c84cf5e1b8c05139e12b377f7555af74e04 Mon Sep 17 00:00:00 2001 From: ParovozikThomas Date: Sat, 14 Mar 2026 13:41:42 +0300 Subject: [PATCH 1/3] queue: first attempt --- .../tasks/queue/BoundedBlockingQueue.java | 53 +++++++++++++++++-- 1 file changed, 48 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 5c686cff..4bbdeb10 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 @@ -2,24 +2,67 @@ public class BoundedBlockingQueue { + private final Object[] array; + int capacity; + int tail; + int head; + int fill; + public BoundedBlockingQueue(int capacity) { + if (capacity <= 0) { + throw new IllegalArgumentException(); + } + this.array = new Object[capacity]; + this.capacity = capacity; + this.head = capacity - 1; + this.tail = this.head; + this.fill = 0; } - public void put(T item) { + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException(); + } + synchronized (this) { + while (fill == capacity) { + this.wait(); + } + + tail = (tail + 1) % capacity; + fill++; + this.notifyAll(); + + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (this) { + while (fill == 0) { + wait(); + } + + T item = (T) array[head]; + array[head] = null; + head = (head + 1) % capacity; + fill--; + + this.notifyAll(); + return item; + + } + + } public int size() { - return 0; + return fill; } public int capacity() { - return 0; + + return capacity; } } From b48245f0adf21715727b30b004e828ec83ca6196 Mon Sep 17 00:00:00 2001 From: ParovozikThomas Date: Sat, 14 Mar 2026 13:44:47 +0300 Subject: [PATCH 2/3] queue: second attempt --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 4bbdeb10..54928e5f 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 @@ -16,8 +16,8 @@ public BoundedBlockingQueue(int capacity) { this.array = new Object[capacity]; this.capacity = capacity; - this.head = capacity - 1; - this.tail = this.head; + this.head = 0; + this.tail = 0; this.fill = 0; } From 2c2d110aed08511373714377019639169ec71d30 Mon Sep 17 00:00:00 2001 From: ParovozikThomas Date: Sat, 14 Mar 2026 13:47:15 +0300 Subject: [PATCH 3/3] queue: 3-rd attempt --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 54928e5f..609a2698 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 @@ -29,7 +29,7 @@ public void put(T item) throws InterruptedException { while (fill == capacity) { this.wait(); } - + array[tail] = item; tail = (tail + 1) % capacity; fill++; this.notifyAll();