From 59afe472dc87e8d459e2b039c5d840bc0b569495 Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:01:30 +0300 Subject: [PATCH 1/7] queue: solution --- .../tasks/queue/BoundedBlockingQueue.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 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..5e88fb24 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,18 +1,46 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.Queue; +public class BoundedBlockingQueue { + Queue queue; + private int capacity; + private int size = 0; + Object monitor; public BoundedBlockingQueue(int capacity) { - + this.capacity = capacity; } public void put(T item) { - + synchronized (monitor) { + if (size == capacity) { + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + queue.add(item); + size++; + monitor.notifyAll(); + } } public T take() { - return null; + synchronized (monitor) { + if (size == 0) { + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + size--; + monitor.notifyAll(); + return queue.poll(); + } } public int size() { From 504bcc7608c1edab3bcdba3e9463e58cf829d7fb Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:11:33 +0300 Subject: [PATCH 2/7] queue: solution --- .../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 5e88fb24..b59eed02 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 @@ -7,7 +7,7 @@ public class BoundedBlockingQueue { Queue queue; private int capacity; private int size = 0; - Object monitor; + Object monitor = new Object(); public BoundedBlockingQueue(int capacity) { this.capacity = capacity; } From af093d36a2de085d59cd2111c0249395376532bb Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:14:13 +0300 Subject: [PATCH 3/7] queue: solution --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 3 ++- 1 file changed, 2 insertions(+), 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 b59eed02..8f7d17aa 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,10 +1,11 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.ArrayDeque; import java.util.Queue; public class BoundedBlockingQueue { - Queue queue; + ArrayDeque queue = new ArrayDeque<>(); private int capacity; private int size = 0; Object monitor = new Object(); From 4483dedf462ad74a70a82ad704a2f158945370db Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:16:58 +0300 Subject: [PATCH 4/7] queue: solution --- .../tasks/queue/BoundedBlockingQueue.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 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 8f7d17aa..666d0f03 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 @@ -13,14 +13,12 @@ public BoundedBlockingQueue(int capacity) { this.capacity = capacity; } - public void put(T item) { + public void put(T item) throws InterruptedException { synchronized (monitor) { if (size == capacity) { - try { + monitor.wait(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + } queue.add(item); @@ -29,14 +27,12 @@ public void put(T item) { } } - public T take() { + public T take() throws InterruptedException { synchronized (monitor) { - if (size == 0) { - try { + while (size == 0) { + monitor.wait(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + } size--; monitor.notifyAll(); From 98a80862f5e497ef8084ee179cff4ed8fcb9964c Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:19:53 +0300 Subject: [PATCH 5/7] queue: solution --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 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 666d0f03..3715ed23 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 @@ -9,7 +9,9 @@ public class BoundedBlockingQueue { private int capacity; private int size = 0; Object monitor = new Object(); - public BoundedBlockingQueue(int capacity) { + + public BoundedBlockingQueue(int capacity) throws IllegalArgumentException { + if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; } @@ -17,7 +19,7 @@ public void put(T item) throws InterruptedException { synchronized (monitor) { if (size == capacity) { - monitor.wait(); + monitor.wait(); } @@ -31,7 +33,7 @@ public T take() throws InterruptedException { synchronized (monitor) { while (size == 0) { - monitor.wait(); + monitor.wait(); } size--; From 31023b8816d364082d610307faab6153596d4ea3 Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:23:11 +0300 Subject: [PATCH 6/7] queue: solution --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 3 ++- 1 file changed, 2 insertions(+), 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 3715ed23..b2ee2b8c 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 @@ -15,7 +15,8 @@ public BoundedBlockingQueue(int capacity) throws IllegalArgumentException { this.capacity = capacity; } - public void put(T item) throws InterruptedException { + public void put(T item) throws InterruptedException, IllegalArgumentException { + if (item == null) throw new IllegalArgumentException(); synchronized (monitor) { if (size == capacity) { From 3a378a1fad868ab00105a2bc491faf8d50d2c67e Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:24:24 +0300 Subject: [PATCH 7/7] queue: solution --- .../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 b2ee2b8c..053e5f02 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 @@ -44,10 +44,10 @@ public T take() throws InterruptedException { } public int size() { - return 0; + return this.size; } public int capacity() { - return 0; + return this.capacity; } }