import java.time.Duration; import java.time.LocalTime; import java.util.*; public class Main { public static void main(String[] args) { LinkedList list = new LinkedList<>(); LinkedList copyList = new LinkedList<>(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long timeSlice = sc.nextLong(); //to millis for (int i = 0; i < n; i++) { Process process = new Process(sc.next(), sc.nextDouble(), sc.nextDouble()); //to seconds list.add(process); copyList.add(process); } processRoundRobin(copyList, timeSlice); for (Process process : list) { process.completeData(); System.out.println(process); } String[] averages = getAverages(list); System.out.println("ART: " + averages[0] + ", AWT: " + averages[1]); } static String[] getAverages(LinkedList list){ double ART = 0, AWT = 0; for (Process p : list) { AWT += p.WT; ART += p.RT; } ART /= list.size(); AWT /= list.size(); return new String[]{String.format("%.03f", ART), String.format("%.03f", AWT)}; } static void processRoundRobin(LinkedList list, long timeSlice){ list.sort(Comparator.comparing(o -> o.AT)); LinkedList queue = new LinkedList<>(); LocalTime startTime = LocalTime.now(); Thread arriveTimeCheckThread = new Thread(() -> { while (list.size() != 0){ if(list.getFirst().AT <= Duration.between(startTime, LocalTime.now()).toMillis() / 1000.0){ Process process = list.remove(); queue.add(process); } } }); arriveTimeCheckThread.start(); Thread processThread = new Thread(() -> { while (list.size() !=0 || queue.size() != 0){ System.out.print("");// Warning!: don't erase this line, or it will never exit the loop if(queue.size() != 0){ Process process = queue.remove(); process.process(timeSlice); if(process.remainedTime > 0){ queue.add(process); }else { process.FT = Duration.between(startTime, LocalTime.now()).toMillis() / 1000.0; } } } }); processThread.start(); try { arriveTimeCheckThread.join(); processThread.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } static class Process{ String name; double AT, CBT, FT, RT, WT; double remainedTime; @Override public String toString() { return "Process{" + "name='" + name + '\'' + ", AT=" + AT + ", CBT=" + CBT + ", FT=" + String.format("%.03f", FT) + ", RT=" + String.format("%.03f", RT) + ", WT=" + String.format("%.03f", WT) + '}'; } public Process(String name, double AT, double CBT){ this.name = name; this.AT = AT; this.CBT = CBT; this.remainedTime = CBT; } void process(long timeSlice){ LocalTime startTime = LocalTime.now(); try { // System.out.println("timeSlice: " + timeSlice); Thread.sleep(timeSlice); } catch (InterruptedException e) { throw new RuntimeException(e); } LocalTime now = LocalTime.now(); long processTime = Duration.between(startTime, now).toMillis(); // System.out.println(name + " process time:" + processTime); remainedTime -= processTime / 1000.0; // System.out.println(this + " remainedTime:" + remainedTime); } void completeData(){ RT = FT - AT; WT = RT - CBT; } } }