Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.36 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 Server 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.                 // System.out.println("merg" + Integer.toString(id));
  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.                 //System.out.println("incep modificarea");
  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 Server(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.     public void run() {
  86.         waiting = new int[maxQueues + 1];
  87.         threads = new Thread[maxQueues + 1];
  88.         added = new int[n + 1];
  89.  
  90.         for (int i = 0; i < maxQueues; ++i) {
  91.             threads[i] = new Thread(new SimulatingQueue(i));
  92.             threads[i].start();
  93.             waiting[i] = 0;
  94.         }
  95.  
  96.         activeQueues = 0;
  97.  
  98.         for (int i = 0; i < n; ++i)
  99.             System.out.println(clients[i].id);
  100.  
  101.         for (int time = 0; time <= endTime; ++time) {
  102.             for (int i = 0; i < n; ++i) {
  103.                 if (clients[i].tArr == time) {
  104.                     int mx = (int) 1e9;
  105.                     int pos = -1;
  106.                     for (int j = 0; j < maxQueues; j++)
  107.                         if (waiting[j] < mx) {
  108.                             mx = waiting[j];
  109.                             pos = j;
  110.                         }
  111.                     added[i] = 1;
  112.                     totalWaited += mx + clients[i].tServ;
  113.                     queues[pos].add(clients[i]);
  114.                     waiting[pos] += clients[i].tServ;
  115.                 }
  116.             }
  117.  
  118.             print(time);
  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.             //System.out.println("le am trezit");
  137.             try {
  138.                 //System.out.println("main astept");
  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.         for (int i = 0; i < maxQueues; ++i)
  150.             threads[i].stop();
  151.  
  152.     }
  153.  
  154.     public void print(final int time) {
  155.         System.out.println("Time " + Integer.toString(time));
  156.         System.out.print("Waiting clients: ");
  157.         for (int i = 0; i < n; ++i) {
  158.             if (added[i] == 0)
  159.                 System.out.print("(" + Integer.toString(clients[i].id) + "," + Integer.toString(clients[i].tArr) + ","
  160.                         + Integer.toString(clients[i].tServ) + "); ");
  161.         }
  162.         System.out.println();
  163.         for (int i = 0; i < maxQueues; ++i) {
  164.             System.out.print("Queue " + Integer.toString(i + 1) + ": ");
  165.             if (queues[i].size() == 0)
  166.                 System.out.println("closed");
  167.             else {
  168.                 for (int j = 0; j < queues[i].size(); ++j)
  169.                     System.out.print(
  170.                             "(" + Integer.toString(queues[i].get(j).id) + "," + Integer.toString(queues[i].get(j).tArr)
  171.                                     + "," + Integer.toString(queues[i].get(j).tServ) + "); ");
  172.                 System.out.println();
  173.             }
  174.         }
  175.         System.out.println();
  176.     }
  177.  
  178. }
  179.  
  180. class Client {
  181.     public int id;
  182.     public int tArr;
  183.     public int tServ;
  184.  
  185.     public Client(final int _id, final int _tArr, final int _tServ) {
  186.         id = _id;
  187.         tArr = _tArr;
  188.         tServ = _tServ;
  189.     }
  190. }
  191.  
  192. class Simulator {
  193.     public static void main(final String args[]) {
  194.         final File in = new File("input.txt");
  195.         try {
  196.             final Scanner sc = new Scanner(in);
  197.  
  198.             int n, q, tMax, minArr, maxArr, minServ, maxServ;
  199.  
  200.             n = sc.nextInt();
  201.             q = sc.nextInt();
  202.             tMax = sc.nextInt();
  203.  
  204.             String line = sc.nextLine();
  205.             line = sc.nextLine();
  206.             String[] lineVector = line.split(",");
  207.  
  208.             minArr = Integer.parseInt(lineVector[0]);
  209.             maxArr = Integer.parseInt(lineVector[1]);
  210.  
  211.             line = sc.nextLine();
  212.             lineVector = line.split(",");
  213.  
  214.             minServ = Integer.parseInt(lineVector[0]);
  215.             maxServ = Integer.parseInt(lineVector[1]);
  216.  
  217.             sc.close();
  218.  
  219.             // System.out.println(n);
  220.             // System.out.println(q);
  221.             // System.out.println(tMax);
  222.             // System.out.println(minArr);
  223.             // System.out.println(maxArr);
  224.             // System.out.println(minServ);
  225.             // System.out.println(maxServ);
  226.  
  227.             final Client clients[] = new Client[100];
  228.  
  229.             final Random rng = new Random();
  230.  
  231.             for (int i = 0; i < n; ++i) {
  232.                 final int tA = minArr + rng.nextInt((int) (maxArr - minArr + 1));
  233.                 final int tS = minServ + rng.nextInt((int) (maxServ - minServ + 1));
  234.  
  235.                 clients[i] = (new Client(i, tA, tS));
  236.             }
  237.  
  238.             final Server server = new Server(clients, n, tMax, q);
  239.  
  240.             (new Thread(server)).start();
  241.         } catch (final FileNotFoundException e)
  242.         {
  243.             System.out.println("Input file not found");
  244.         }
  245.  
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement