Facebook
From matinshaah, 1 Year ago, written in Java.
Embed
Download Paste or View Raw
Hits: 51
  1. import java.time.Duration;
  2. import java.time.LocalTime;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         LinkedList<Process> list = new LinkedList<>();
  8.         LinkedList<Process> copyList = new LinkedList<>();
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         long timeSlice = sc.nextLong(); //to millis
  12.         for (int i = 0; i < n; i++) {
  13.             Process process = new Process(sc.next(), sc.nextDouble(), sc.nextDouble());  //to seconds
  14.             list.add(process);
  15.             copyList.add(process);
  16.         }
  17.         processRoundRobin(copyList, timeSlice);
  18.  
  19.         for (Process process :
  20.                 list) {
  21.             process.completeData();
  22.             System.out.println(process);
  23.         }
  24.         String[] averages = getAverages(list);
  25.         System.out.println("ART: " + averages[0] + ", AWT: " + averages[1]);
  26.     }
  27.  
  28.     static String[] getAverages(LinkedList<Process> list){
  29.         double ART = 0, AWT = 0;
  30.         for (Process p :
  31.                 list) {
  32.             AWT += p.WT;
  33.             ART += p.RT;
  34.         }
  35.         ART /= list.size();
  36.         AWT /= list.size();
  37.         return new String[]{String.format("%.03f", ART), String.format("%.03f", AWT)};
  38.     }
  39.  
  40.     static void processRoundRobin(LinkedList<Process> list, long timeSlice){
  41.         list.sort(Comparator.comparing(o -> o.AT));
  42.         LinkedList<Process> queue = new LinkedList<>();
  43.         LocalTime startTime = LocalTime.now();
  44.  
  45.         Thread arriveTimeCheckThread = new Thread(() -> {
  46.             while (list.size() != 0){
  47.                 if(list.getFirst().AT <= Duration.between(startTime, LocalTime.now()).toMillis() / 1000.0){
  48.                     Process process = list.remove();
  49.                     queue.add(process);
  50.                 }
  51.             }
  52.         });
  53.         arriveTimeCheckThread.start();
  54.         Thread processThread = new Thread(() -> {
  55.             while (list.size() !=0 || queue.size() != 0){
  56.                 System.out.print("");// Warning!: don't erase this line, or it will never exit the loop
  57.                 if(queue.size() != 0){
  58.                     Process process = queue.remove();
  59.                     process.process(timeSlice);
  60.                     if(process.remainedTime > 0){
  61.                         queue.add(process);
  62.                     }else {
  63.                         process.FT = Duration.between(startTime, LocalTime.now()).toMillis() / 1000.0;
  64.                     }
  65.                 }
  66.             }
  67.         });
  68.         processThread.start();
  69.         try {
  70.             arriveTimeCheckThread.join();
  71.             processThread.join();
  72.         } catch (InterruptedException e) {
  73.             throw new RuntimeException(e);
  74.         }
  75.     }
  76.  
  77.     static class Process{
  78.         String name;
  79.         double AT, CBT, FT, RT, WT;
  80.         double remainedTime;
  81.  
  82.         @Override
  83.         public String toString() {
  84.             return "Process{" +
  85.                     "name='" + name + '\'' +
  86.                     ", AT=" + AT +
  87.                     ", CBT=" + CBT +
  88.                     ", FT=" + String.format("%.03f", FT) +
  89.                     ", RT=" + String.format("%.03f", RT) +
  90.                     ", WT=" + String.format("%.03f", WT) +
  91.                     '}';
  92.         }
  93.  
  94.         public Process(String name, double AT, double CBT){
  95.             this.name = name;
  96.             this.AT = AT;
  97.             this.CBT = CBT;
  98.             this.remainedTime = CBT;
  99.         }
  100.  
  101.         void process(long timeSlice){
  102.             LocalTime startTime = LocalTime.now();
  103.             try {
  104. //                System.out.println("timeSlice: " + timeSlice);
  105.                 Thread.sleep(timeSlice);
  106.             } catch (InterruptedException e) {
  107.                 throw new RuntimeException(e);
  108.             }
  109.             LocalTime now = LocalTime.now();
  110.             long processTime = Duration.between(startTime, now).toMillis();
  111. //            System.out.println(name + " process time:" + processTime);
  112.             remainedTime -= processTime / 1000.0;
  113. //            System.out.println(this + " remainedTime:" + remainedTime);
  114.         }
  115.  
  116.         void completeData(){
  117.             RT = FT - AT;
  118.             WT = RT - CBT;
  119.  
  120.         }
  121.     }
  122. }