Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,24 @@ public void attachMonitor(StreamingMonitor monitor) {

@Override
public void run() {
// Writer threads are intentionally infinite for the task contract.
while (true) {
output.print(message);
onTick.run();
synchronized (monitor) {
while (true) {
if (monitor.current>=monitor.total){
monitor.notifyAll();
return;
}
if ((monitor.current % monitor.N) + 1 == id) {
output.print(message);
onTick.run();
monitor.current++;
monitor.notifyAll();
}
try {
monitor.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package hse.java.lectures.lecture6.tasks.synchronizer;

public class StreamingMonitor {
// impl your sync here
int N, ticksPerWriter;
int current = 0;
int total;
StreamingMonitor(int N,int ticksPerWriter){
this.N=N;
this.ticksPerWriter=ticksPerWriter;
total=N*ticksPerWriter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ public Synchronizer(List<StreamWriter> tasks, int ticksPerWriter) {
* in strict ascending id order.
*/
public void execute() {
// add monitor and sync
StreamingMonitor monitor = new StreamingMonitor(tasks.size(),ticksPerWriter);
for (StreamWriter writer : tasks) {
writer.attachMonitor(monitor);
}
for (StreamWriter writer : tasks) {
Thread worker = new Thread(writer, "stream-writer-" + writer.getId());
worker.setDaemon(true);
worker.start();
}
synchronized (monitor) {
while (monitor.current < monitor.total) {
try {
monitor.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
}

}
Loading