Muraveva_A_B_Synchronizer#151
Hidden character warning
Conversation
|
Поздравляем! Пройден усложненный набор тестов для задачи |
There was a problem hiding this comment.
Pull request overview
Implements the lecture6 “synchronizer” and “bounded blocking queue” tasks by adding a monitor-based coordination mechanism for ordered printing and a synchronized/wait/notifyAll FIFO queue implementation.
Changes:
- Added
StreamingMonitorand wired it intoSynchronizer.execute()to enforce strict ascendingidoutput ordering for a bounded number of ticks. - Updated
StreamWriterto consult the monitor before printing and to notify the monitor after each tick. - Implemented
BoundedBlockingQueue<T>with capacity bounds, blockingput/take, and FIFO semantics withoutjava.util.concurrent.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/Synchronizer.java | Builds sorted writer id order, attaches a shared monitor, starts writer threads, and waits for completion. |
| src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamingMonitor.java | New shared monitor managing turn-taking and completion tracking via synchronized/wait/notifyAll. |
| src/main/java/hse/java/lectures/lecture6/tasks/synchronizer/StreamWriter.java | Adds monitor gating and completion signaling around each printed tick. |
| src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | Implements a bounded blocking FIFO queue with basic argument validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| int[] sortedIds = tasks.stream() | ||
| .mapToInt(StreamWriter::getId) | ||
| .sorted() | ||
| .toArray(); | ||
|
|
||
| StreamingMonitor monitor = new StreamingMonitor(sortedIds, ticksPerWriter); | ||
|
|
There was a problem hiding this comment.
execute() can deadlock when tasks is empty (no worker will ever call tickDone(), so awaitDone() waits forever). Add explicit argument validation (e.g., require tasks.size() >= 1/2 per task contract and ticksPerWriter >= 0) and fail fast with a clear exception before constructing the monitor.
| private int currentIdx = 0; | ||
| private int completedTicks = 0; | ||
|
|
||
| public StreamingMonitor(int[] sortedIds, int targetTicks) { |
There was a problem hiding this comment.
sortedIds is used as sortedIds[currentIdx] in waitTurn(), but the constructor doesn't guard against sortedIds.length == 0, which would cause an ArrayIndexOutOfBoundsException or a hang depending on call path. Consider validating sortedIds (non-null, non-empty) and targetTicks (>= 0) in the constructor and throwing an IllegalArgumentException with a helpful message.
| public StreamingMonitor(int[] sortedIds, int targetTicks) { | |
| public StreamingMonitor(int[] sortedIds, int targetTicks) { | |
| if (sortedIds == null || sortedIds.length == 0) { | |
| throw new IllegalArgumentException("sortedIds must be non-null and non-empty"); | |
| } | |
| if (targetTicks < 0) { | |
| throw new IllegalArgumentException("targetTicks must be non-negative"); | |
| } |
| while (true) { | ||
| if (!monitor.waitTurn(id)) { | ||
| break; | ||
| } |
There was a problem hiding this comment.
Current implementation exits the writer thread once waitTurn() returns false. This conflicts with the task contract in task.md that describes StreamWriter.run() as an infinite loop; a safer approach is usually to keep the thread alive but block it after completion (or otherwise align the contract + implementation).
| output.print(message); | ||
| onTick.run(); | ||
| monitor.tickDone(); |
There was a problem hiding this comment.
If output.print(...) or (more likely) onTick.run() throws a runtime exception, tickDone() will never be called and all other writers (and execute() via awaitDone()) can block indefinitely. Call monitor.tickDone() in a finally block, and consider how to handle the exception (e.g., propagate via a shared error flag and unblock waiters).
| output.print(message); | |
| onTick.run(); | |
| monitor.tickDone(); | |
| try { | |
| output.print(message); | |
| onTick.run(); | |
| } finally { | |
| monitor.tickDone(); | |
| } |
No description provided.