Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.BrokenBarrierException;
  3. import java.util.concurrent.CyclicBarrier;
  4. import java.util.regex.Pattern;
  5. import java.io.*;
  6.  
  7.  
  8. class Manager implements Runnable
  9. {
  10.  
  11.     class SimulatingQueue implements Runnable
  12.     {
  13.         int id;
  14.  
  15.         public SimulatingQueue(final int _id) {
  16.             id = _id;
  17.         }
  18.  
  19.         public void run() {
  20.             while (true) {
  21.              
  22.                 synchronized (this) {
  23.                     while (queues[id].size() == 0) {
  24.                         try {
  25.                             this.wait(100);
  26.                         } catch (final InterruptedException e) {
  27.                         }
  28.                     }
  29.                 }
  30.  
  31.                 try {
  32.                     Thread.sleep(15); // so that it has enough time to update the barrier in main process
  33.                 } catch (final InterruptedException e) {
  34.  
  35.                 }
  36.  
  37.      
  38.                 final Client curr = queues[id].get(0);
  39.  
  40.                 curr.tServ--;
  41.  
  42.                 queues[id].setElementAt(curr, 0);
  43.  
  44.                 waiting[id]--;
  45.                 if (queues[id].get(0).tServ == 0)
  46.                     queues[id].remove(queues[id].get(0));
  47.                 try {
  48.                     barrier.await();
  49.                 } catch (InterruptedException | BrokenBarrierException e) {
  50.  
  51.                 }
  52.  
  53.             }
  54.         }
  55.     }
  56.  
  57.     Vector<Client> queues[];
  58.     int waiting[];
  59.     Thread threads[];
  60.     int activeQueues;
  61.     int added[];
  62.    
  63.     Client clients[];
  64.     int endTime;
  65.     int maxQueues;
  66.     int n;
  67.     public static CyclicBarrier barrier;
  68.     public int totalWaited = 0;
  69.  
  70.     public Manager(final Client[] _clients, final int _n, final int _endTime, final int _maxQueues) {
  71.         endTime = _endTime;
  72.         maxQueues = _maxQueues;
  73.         n = _n;
  74.  
  75.         clients = new Client[n + 1];
  76.         queues = new Vector[maxQueues + 1];
  77.  
  78.         for (int i = 0; i < maxQueues; ++i)
  79.             queues[i] = new Vector(0);
  80.  
  81.         for (int i = 0; i < n; ++i)
  82.             clients[i] = new Client(_clients[i].id, _clients[i].tArr, _clients[i].tServ);
  83.     }
  84.  
  85.     @SuppressWarnings("deprecation")
  86.     public void run() {
  87.         waiting = new int[maxQueues + 1];
  88.         threads = new Thread[maxQueues + 1];
  89.         added = new int[n + 1];
  90.  
  91.         for (int i = 0; i < maxQueues; ++i) {
  92.             threads[i] = new Thread(new SimulatingQueue(i));
  93.             threads[i].start();
  94.             waiting[i] = 0;
  95.         }
  96.  
  97.         activeQueues = 0;
  98.  
  99.         //for (int i = 0; i < n; ++i)
  100.             //System.out.println(clients[i].id);
  101.  
  102.         for (int time = 0; time <= endTime; ++time) {
  103.             for (int i = 0; i < n; ++i) {
  104.                 if (clients[i].tArr == time) {
  105.                     int mx = (int) 1e9;
  106.                     int pos = -1;
  107.                     for (int j = 0; j < maxQueues; j++)
  108.                         if (waiting[j] < mx) {
  109.                             mx = waiting[j];
  110.                             pos = j;
  111.                         }
  112.                     added[i] = 1;
  113.                     totalWaited += mx + clients[i].tServ;
  114.                     queues[pos].add(clients[i]);
  115.                     waiting[pos] += clients[i].tServ;
  116.                 }
  117.             }
  118.  
  119.            
  120.             activeQueues = 0;
  121.  
  122.             for (int i = 0; i < maxQueues; ++i)
  123.                 if (queues[i].size() != 0) {
  124.                     activeQueues++;
  125.                 }
  126.  
  127.             barrier = new CyclicBarrier(activeQueues + 1);
  128.  
  129.             for (int i = 0; i < maxQueues; ++i)
  130.                 if (queues[i].size() != 0) {
  131.                     synchronized (threads[i]) {
  132.                         threads[i].notify();
  133.                     }
  134.                 }
  135.  
  136.        
  137.             try {
  138.            
  139.  
  140.                 barrier.await();
  141.             } catch (InterruptedException | BrokenBarrierException e) {
  142.  
  143.             }
  144.             barrier.reset();
  145.         }
  146.  
  147.         System.out.println("Average waiting time: " + Float.toString((float) (totalWaited * 1.0 / n)));
  148.        
  149.        
  150.        
  151.         for (int i = 0; i < maxQueues; ++i)
  152.             threads[i].stop();
  153.  
  154.     }
  155.  
  156.     public String print(final int time) {
  157.         String str = new String();
  158.         str.concat("Time " + Integer.toString(time) + '\n');
  159.         str.concat("Waiting clients: ");
  160.        // System.out.println("Time " + Integer.toString(time));
  161.         //System.out.print("Waiting clients: ");
  162.         for (int i = 0; i < n; ++i) {
  163.             if (added[i] == 0)
  164.                // System.out.print("(" + Integer.toString(clients[i].id) + "," + Integer.toString(clients[i].tArr) + ","
  165.                   //      + Integer.toString(clients[i].tServ) + "); ");
  166.             str.concat("(" + Integer.toString(clients[i].id) + "," + Integer.toString(clients[i].tArr) + ","
  167.                         + Integer.toString(clients[i].tServ) + "); ");
  168.         }
  169.        // System.out.println();
  170.         str.concat("'\n'");
  171.         for (int i = 0; i < maxQueues; ++i) {
  172.             //System.out.print("Queue " + Integer.toString(i + 1) + ": ");
  173.             str.concat("Queue " + Integer.toString(i + 1) + ": ");
  174.             if (queues[i].size() == 0)
  175.                 //System.out.print("closed");
  176.                 str.concat("closed");
  177.             else {
  178.                 for (int j = 0; j < queues[i].size(); ++j)
  179.                     //System.out.print(
  180.                          //   "(" + Integer.toString(queues[i].get(j).id) + "," + Integer.toString(queues[i].get(j).tArr)
  181.                          //           + "," + Integer.toString(queues[i].get(j).tServ) + "); ");
  182.                     str.concat(
  183.                             "(" + Integer.toString(queues[i].get(j).id) + "," + Integer.toString(queues[i].get(j).tArr)
  184.                             + "," + Integer.toString(queues[i].get(j).tServ) + "); ");
  185.                 //System.out.println();
  186.                 str.concat("'\n'");
  187.             }
  188.         }
  189.         //System.out.println();
  190.         str.concat("'\n'");
  191.        
  192.         return str;
  193.     }
  194.    
  195. }
  196.  
  197. class Client {
  198.     int id;
  199.     int tArr;
  200.     int tServ;
  201.  
  202.     public Client(final int _id, final int _tArr, final int _tServ) {
  203.         id = _id;
  204.         tArr = _tArr;
  205.         tServ = _tServ;
  206.     }
  207. }
  208.  
  209. class MainClass {
  210.     public static void main(final String args[]) {
  211.        
  212.         final File in = new File("input.txt");
  213.         try {
  214.             final Scanner sc = new Scanner(in);
  215.  
  216.             int n, q, tMax, minArr, maxArr, minServ, maxServ;
  217.  
  218.             n = sc.nextInt();
  219.             q = sc.nextInt();
  220.             tMax = sc.nextInt();
  221.  
  222.             String line = sc.nextLine();
  223.             line = sc.nextLine();
  224.             String[] lineVector = line.split(",");
  225.  
  226.             minArr = Integer.parseInt(lineVector[0]);
  227.             maxArr = Integer.parseInt(lineVector[1]);
  228.  
  229.             line = sc.nextLine();
  230.             lineVector = line.split(",");
  231.  
  232.             minServ = Integer.parseInt(lineVector[0]);
  233.             maxServ = Integer.parseInt(lineVector[1]);
  234.  
  235.             sc.close();
  236.  
  237.  
  238.             final Client clients[] = new Client[1000];
  239.  
  240.             final Random rng = new Random();
  241.  
  242.             for (int i = 0; i < n; ++i) {
  243.                 final int tA = minArr + rng.nextInt((int) (maxArr - minArr + 1));
  244.                 final int tS = minServ + rng.nextInt((int) (maxServ - minServ + 1));
  245.  
  246.                 clients[i] = (new Client(i, tA, tS));
  247.             }
  248.  
  249.             final Manager man = new Manager(clients, n, tMax, q);
  250.            
  251.             (new Thread(man)).start();
  252.  
  253.         } catch (final FileNotFoundException e)
  254.         {
  255.             System.out.println("Input file not found");
  256.         }
  257.            
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement