Skip to content

Muraveva_A_B_Synchronizer#151

Open
MuravAna wants to merge 2 commits intoLevinMK23:mainfrom
MuravAna:Muraveva_A_B_praс_6_sync

Hidden character warning

The head ref may contain hidden characters: "Muraveva_A_B_pra\u0441_6_sync"
Open

Muraveva_A_B_Synchronizer#151
MuravAna wants to merge 2 commits intoLevinMK23:mainfrom
MuravAna:Muraveva_A_B_praс_6_sync

Conversation

@MuravAna
Copy link

No description provided.

Copilot AI review requested due to automatic review settings March 21, 2026 11:28
@github-actions
Copy link

Поздравляем!

Пройден усложненный набор тестов для задачи synchronizer.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 StreamingMonitor and wired it into Synchronizer.execute() to enforce strict ascending id output ordering for a bounded number of ticks.
  • Updated StreamWriter to consult the monitor before printing and to notify the monitor after each tick.
  • Implemented BoundedBlockingQueue<T> with capacity bounds, blocking put/take, and FIFO semantics without java.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.

Comment on lines +21 to +27
int[] sortedIds = tasks.stream()
.mapToInt(StreamWriter::getId)
.sorted()
.toArray();

StreamingMonitor monitor = new StreamingMonitor(sortedIds, ticksPerWriter);

Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
private int currentIdx = 0;
private int completedTicks = 0;

public StreamingMonitor(int[] sortedIds, int targetTicks) {
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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");
}

Copilot uses AI. Check for mistakes.
Comment on lines 29 to +32
while (true) {
if (!monitor.waitTurn(id)) {
break;
}
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines 33 to +35
output.print(message);
onTick.run();
monitor.tickDone();
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
output.print(message);
onTick.run();
monitor.tickDone();
try {
output.print(message);
onTick.run();
} finally {
monitor.tickDone();
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants