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,20 @@ 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();
try
{
while (true)
{
monitor.waitWorker(this.id);
output.print(message);
onTick.run();

monitor.next();
}
}
catch (InterruptedException e)
{
// И тут тоже, что делать стоит....
}
}

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

import java.util.List;

public class StreamingMonitor {
// impl your sync here

private final int maxTicks;
private final List<Integer> ids;

private int curIndex = 0;
private int curTick = 0;

public StreamingMonitor(List<Integer> ids, int maxTicks)
{
this.ids = ids;
this.maxTicks = maxTicks;
}


public synchronized void waitWorker(int id) throws InterruptedException
{
while (curTick >= maxTicks || ids.get(curIndex) != id)
{
wait();
}
}

public synchronized void next()
{
curTick++;
curIndex = (curIndex + 1) % ids.size();
notifyAll();
}

public synchronized void waitFinish() throws InterruptedException
{
while (curTick < maxTicks)
{
wait();
}
}
}

Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
package hse.java.lectures.lecture6.tasks.synchronizer;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Synchronizer {
public class Synchronizer
{

public static final int DEFAULT_TICKS_PER_WRITER = 10;
private final List<StreamWriter> tasks;
private final int ticksPerWriter;

public Synchronizer(List<StreamWriter> tasks) {
public Synchronizer(List<StreamWriter> tasks)
{
this(tasks, DEFAULT_TICKS_PER_WRITER);
}

public Synchronizer(List<StreamWriter> tasks, int ticksPerWriter) {
public Synchronizer(List<StreamWriter> tasks, int ticksPerWriter)
{
this.tasks = tasks;
this.ticksPerWriter = ticksPerWriter;
}

/**
* Starts infinite writer threads and waits until each writer prints exactly ticksPerWriter ticks
* Starts infinite writer threads and waits until each writer prints exactly ticksPerWriter
* ticks
* in strict ascending id order.
*/
public void execute() {
// add monitor and sync
for (StreamWriter writer : tasks) {
public void execute()
{

List<StreamWriter> tasksCopy = new ArrayList<>(tasks);
tasksCopy.sort(Comparator.comparingInt(StreamWriter::getId));

List<Integer> taskIds = tasksCopy.stream().map(StreamWriter::getId).toList();

StreamingMonitor monitor = new StreamingMonitor(taskIds, tasks.size() * ticksPerWriter);
for (StreamWriter writer : tasksCopy)
{
writer.attachMonitor(monitor);
Thread worker = new Thread(writer, "stream-writer-" + writer.getId());
worker.setDaemon(true);
worker.start();
}

try
{
monitor.waitFinish();
} catch (InterruptedException e)
{
// я не знаю что здесь стоит сделать =(
}
}

}
Loading