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
4 changes: 4 additions & 0 deletions src/HomeWork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import java.time.LocalDate;

public record HomeWork(String data, Integer id, LocalDate deadLine) {
}
56 changes: 56 additions & 0 deletions src/HomeWorkCheckSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.concurrent.*;
import java.util.function.Consumer;

public class HomeWorkCheckSystem extends Thread {

private final ExecutorService executorService = Executors.newFixedThreadPool(5);
private final PriorityBlockingQueue<HomeWork> hwQueue = new PriorityBlockingQueue<>(10, new HomeWorkComparator());
private final ConcurrentHashMap<Integer, Consumer<HomeWorkResult>> hwCheckCallbacks = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, HomeWorkResult> results = new ConcurrentHashMap<>();

@Override
public void run() {
super.run();
try {
while (true) {
HomeWork needToCheck = hwQueue.poll();
if (needToCheck == null) {
System.out.println("Waiting for new homeworks");
sleep(1000);
continue;
}
System.out.println("Pushing hw " + needToCheck.id() + " with deadline: " + needToCheck.deadLine() + " to check" );
processHomeWorkCheck(needToCheck);
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}

public void sendHomeWorkForCheck(HomeWork hw, Consumer<HomeWorkResult> callback) {
System.out.println("New homework: " + hw.id() + " with deadline: " + hw.deadLine());
hwQueue.add(hw);
hwCheckCallbacks.put(hw.id(), callback);
}

public HomeWorkResult getHomeWorkResult(Integer id) {
return results.get(id);
}

private void processHomeWorkCheck(HomeWork hw) throws InterruptedException, ExecutionException {
var future = executorService.submit(() -> {
try {
System.out.println("Processing hw check: " + hw.id());
sleep(1000);
double randNumber = Math.random();
double score = randNumber * 100;
HomeWorkResult hwResult = new HomeWorkResult(hw, score, "Some description...");
results.put(hw.id(), hwResult);
hwCheckCallbacks.get(hw.id()).accept(hwResult);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}

}
10 changes: 10 additions & 0 deletions src/HomeWorkComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import java.util.Comparator;

public class HomeWorkComparator implements Comparator<HomeWork> {

@Override
public int compare(HomeWork o1, HomeWork o2) {
return o1.deadLine().compareTo(o2.deadLine());
}

}
3 changes: 3 additions & 0 deletions src/HomeWorkResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public record HomeWorkResult(HomeWork hw, Double score, String description) {

}
10 changes: 10 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Main {
public static void main(String[] args) {
HomeWorkCheckSystem system = new HomeWorkCheckSystem();
system.start();
for (var i = 0; i < 15; i++) {
Student st = new Student(system, i);
st.start();
}
}
}
58 changes: 58 additions & 0 deletions src/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;

public class Student extends Thread {

private final Integer id;

private final HomeWorkCheckSystem checkSystem;

public Student(HomeWorkCheckSystem checkSystem, Integer id) {
this.checkSystem = checkSystem;
this.id = id;
}

@Override
public void run() {
super.run();
try {
while (true) {
sleep(2000);
if (Math.random() < 0.25) {
sleep(3000);
continue;
}
HomeWork hw = processHomeWork();
System.out.println("Student " + id + " sending homework: " + hw.id() + " with deadline: " + hw.deadLine());
Consumer<HomeWorkResult> callback = homeWorkResult -> System.out.println("Homework " + hw.id() + " result, with score: " + homeWorkResult.score());
checkSystem.sendHomeWorkForCheck(hw, callback);
}
} catch (
InterruptedException e) {
throw new RuntimeException(e);
}
}

private HomeWorkResult getResult(HomeWork hw) {
HomeWorkResult hwResult = checkSystem.getHomeWorkResult(hw.id());
System.out.println("Received result of homework " + hw.id() + " with score: " + hwResult.score());
return hwResult;
}

private HomeWork processHomeWork() {
Random ran = new Random();
int homeworkId = ran.nextInt();

LocalDate startDate = LocalDate.of(2023, 12, 1);
LocalDate endDate = LocalDate.of(2024, 5, 31);
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
long randomNumberOfDays = ThreadLocalRandom.current().nextLong(daysBetween + 1);
LocalDate deadline = startDate.plusDays(randomNumberOfDays);

return new HomeWork("SomeData", homeworkId, deadline);
}
}