From 5de9bca11c1fe65350c2e602ff97886c9b47b43e Mon Sep 17 00:00:00 2001 From: kmelnikovmh Date: Sun, 15 Mar 2026 23:30:10 +0300 Subject: [PATCH 1/3] queue: mvp v1.0 --- .../tasks/queue/BoundedBlockingQueue.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 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..710dfc0c 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,40 @@ public class BoundedBlockingQueue { + int head, tail, count; + Object[] items; - public BoundedBlockingQueue(int capacity) { - + BoundedBlockingQueue(int capacity) { + items = new Object[capacity]; } - public void put(T item) { - + synchronized void put(T item) throws InterruptedException { + while (count == items.length) { + wait(); + } + items[tail] = item; + tail = (tail + 1) % items.length; + ++count; + notifyAll(); } - public T take() { - return null; + synchronized T take() throws InterruptedException { + while (count == 0) { + wait(); + } + T item = (T)items[head]; + items[head] = null; + head = (head + 1) % items.length; + --count; + notifyAll(); + return item; } - public int size() { - return 0; + synchronized int size() { + return count; } - public int capacity() { - return 0; + private int capacity() { + return items.length; } } From 6ea1cdc80f4e26b59c5e60b6ccb05b4c0917f8cc Mon Sep 17 00:00:00 2001 From: kmelnikovmh Date: Sun, 15 Mar 2026 23:34:34 +0300 Subject: [PATCH 2/3] queue: mvp v1.1 --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 10 +++++----- 1 file changed, 5 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 710dfc0c..582726ea 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 @@ -5,11 +5,11 @@ public class BoundedBlockingQueue { int head, tail, count; Object[] items; - BoundedBlockingQueue(int capacity) { + public BoundedBlockingQueue(int capacity) { items = new Object[capacity]; } - synchronized void put(T item) throws InterruptedException { + public synchronized void put(T item) throws InterruptedException { while (count == items.length) { wait(); } @@ -19,7 +19,7 @@ synchronized void put(T item) throws InterruptedException { notifyAll(); } - synchronized T take() throws InterruptedException { + public synchronized T take() throws InterruptedException { while (count == 0) { wait(); } @@ -31,11 +31,11 @@ synchronized T take() throws InterruptedException { return item; } - synchronized int size() { + public synchronized int size() { return count; } - private int capacity() { + public int capacity() { return items.length; } } From 9ef5d44548f89c2d29e9c26bce4245516e2f1814 Mon Sep 17 00:00:00 2001 From: kmelnikovmh Date: Sun, 15 Mar 2026 23:37:10 +0300 Subject: [PATCH 3/3] queue: mvp v1.2 --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 3 +++ 1 file changed, 3 insertions(+) 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 582726ea..6549806b 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 @@ -6,6 +6,9 @@ public class BoundedBlockingQueue { Object[] items; public BoundedBlockingQueue(int capacity) { + if (capacity <= 0) { + throw new IllegalArgumentException(); + } items = new Object[capacity]; }