From 59afe472dc87e8d459e2b039c5d840bc0b569495 Mon Sep 17 00:00:00 2001 From: toximu Date: Sat, 14 Mar 2026 13:01:30 +0300 Subject: [PATCH 1/8] 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/8] 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/8] 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/8] 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/8] 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/8] 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/8] 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; } } From 4fb9c5259c2b4da3ea266363715998bc88d69b14 Mon Sep 17 00:00:00 2001 From: toximu Date: Fri, 20 Mar 2026 18:20:44 +0300 Subject: [PATCH 8/8] synchronizer: solution --- .../tasks/synchronizer/StreamWriter.java | 13 ++++++-- .../tasks/synchronizer/StreamingMonitor.java | 31 ++++++++++++++++++- .../tasks/synchronizer/Synchronizer.java | 17 ++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamWriter.java b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamWriter.java index fedb5e66..042777db 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamWriter.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamWriter.java @@ -1,6 +1,7 @@ package hse.java.lectures.lecture6.tasks.synchronizer; import lombok.Getter; +import lombok.SneakyThrows; import java.io.PrintStream; @@ -24,12 +25,20 @@ public void attachMonitor(StreamingMonitor monitor) { this.monitor = monitor; } + @SneakyThrows @Override public void run() { // Writer threads are intentionally infinite for the task contract. while (true) { - output.print(message); - onTick.run(); + synchronized (monitor) { + while (!monitor.isWorking || !(monitor.nowWriter() == id)) { + monitor.wait(); + } + output.print(message); + onTick.run(); + monitor.next(); + monitor.notifyAll(); + } } } diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamingMonitor.java b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamingMonitor.java index 68e8f279..7654a60d 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamingMonitor.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamingMonitor.java @@ -1,5 +1,34 @@ package hse.java.lectures.lecture6.tasks.synchronizer; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + public class StreamingMonitor { - // impl your sync here + int shouldPrintNow = 0; + List ids; + boolean isWorking = true; + int ticksDone = 0; + int tickNeeded = 0; + StreamingMonitor(List ids, int tickNeeded) { + this.ids = new ArrayList<>(ids); + this.ids.sort(Comparator.naturalOrder()); + this.tickNeeded = tickNeeded; + } + + Integer nowWriter() { + return ids.get(shouldPrintNow); + } + + void next() { + shouldPrintNow++; + if (shouldPrintNow == ids.size()) { + shouldPrintNow = 0; + ticksDone++; + } + + if (ticksDone == tickNeeded) { + isWorking = false; + } + } } diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/Synchronizer.java b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/Synchronizer.java index 3cb8aded..20912bab 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/Synchronizer.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/Synchronizer.java @@ -1,11 +1,14 @@ package hse.java.lectures.lecture6.tasks.synchronizer; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Comparator; import java.util.List; public class Synchronizer { public static final int DEFAULT_TICKS_PER_WRITER = 10; - private final List tasks; + private List tasks = List.of(); private final int ticksPerWriter; public Synchronizer(List tasks) { @@ -15,19 +18,29 @@ public Synchronizer(List tasks) { public Synchronizer(List tasks, int ticksPerWriter) { this.tasks = tasks; this.ticksPerWriter = ticksPerWriter; + monitor = new StreamingMonitor(tasks.stream() + .map(StreamWriter::getId).toList(), ticksPerWriter); } /** * Starts infinite writer threads and waits until each writer prints exactly ticksPerWriter ticks * in strict ascending id order. */ - public void execute() { + private final StreamingMonitor monitor; + public void execute() throws InterruptedException { // add monitor and sync for (StreamWriter writer : tasks) { + writer.attachMonitor(monitor); Thread worker = new Thread(writer, "stream-writer-" + writer.getId()); worker.setDaemon(true); worker.start(); } + + synchronized (monitor) { + while (monitor.isWorking) { + monitor.wait(); + } + } } }