Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- import java.util.regex.Pattern;
- import java.io.*;
- class Manager implements Runnable
- {
- class SimulatingQueue implements Runnable
- {
- int id;
- public SimulatingQueue(final int _id) {
- id = _id;
- }
- public void run() {
- while (true) {
- synchronized (this) {
- while (queues[id].size() == 0) {
- try {
- this.wait(100);
- } catch (final InterruptedException e) {
- }
- }
- }
- try {
- Thread.sleep(15); // so that it has enough time to update the barrier in main process
- } catch (final InterruptedException e) {
- }
- final Client curr = queues[id].get(0);
- curr.tServ--;
- queues[id].setElementAt(curr, 0);
- waiting[id]--;
- if (queues[id].get(0).tServ == 0)
- queues[id].remove(queues[id].get(0));
- try {
- barrier.await();
- } catch (InterruptedException | BrokenBarrierException e) {
- }
- }
- }
- }
- Vector<Client> queues[];
- int waiting[];
- Thread threads[];
- int activeQueues;
- int added[];
- Client clients[];
- int endTime;
- int maxQueues;
- int n;
- public static CyclicBarrier barrier;
- public int totalWaited = 0;
- public Manager(final Client[] _clients, final int _n, final int _endTime, final int _maxQueues) {
- endTime = _endTime;
- maxQueues = _maxQueues;
- n = _n;
- clients = new Client[n + 1];
- queues = new Vector[maxQueues + 1];
- for (int i = 0; i < maxQueues; ++i)
- queues[i] = new Vector(0);
- for (int i = 0; i < n; ++i)
- clients[i] = new Client(_clients[i].id, _clients[i].tArr, _clients[i].tServ);
- }
- @SuppressWarnings("deprecation")
- public void run() {
- waiting = new int[maxQueues + 1];
- threads = new Thread[maxQueues + 1];
- added = new int[n + 1];
- for (int i = 0; i < maxQueues; ++i) {
- threads[i] = new Thread(new SimulatingQueue(i));
- threads[i].start();
- waiting[i] = 0;
- }
- activeQueues = 0;
- //for (int i = 0; i < n; ++i)
- //System.out.println(clients[i].id);
- for (int time = 0; time <= endTime; ++time) {
- for (int i = 0; i < n; ++i) {
- if (clients[i].tArr == time) {
- int mx = (int) 1e9;
- int pos = -1;
- for (int j = 0; j < maxQueues; j++)
- if (waiting[j] < mx) {
- mx = waiting[j];
- pos = j;
- }
- added[i] = 1;
- totalWaited += mx + clients[i].tServ;
- queues[pos].add(clients[i]);
- waiting[pos] += clients[i].tServ;
- }
- }
- activeQueues = 0;
- for (int i = 0; i < maxQueues; ++i)
- if (queues[i].size() != 0) {
- activeQueues++;
- }
- barrier = new CyclicBarrier(activeQueues + 1);
- for (int i = 0; i < maxQueues; ++i)
- if (queues[i].size() != 0) {
- synchronized (threads[i]) {
- threads[i].notify();
- }
- }
- try {
- barrier.await();
- } catch (InterruptedException | BrokenBarrierException e) {
- }
- barrier.reset();
- }
- System.out.println("Average waiting time: " + Float.toString((float) (totalWaited * 1.0 / n)));
- for (int i = 0; i < maxQueues; ++i)
- threads[i].stop();
- }
- public String print(final int time) {
- String str = new String();
- str.concat("Time " + Integer.toString(time) + '\n');
- str.concat("Waiting clients: ");
- // System.out.println("Time " + Integer.toString(time));
- //System.out.print("Waiting clients: ");
- for (int i = 0; i < n; ++i) {
- if (added[i] == 0)
- // System.out.print("(" + Integer.toString(clients[i].id) + "," + Integer.toString(clients[i].tArr) + ","
- // + Integer.toString(clients[i].tServ) + "); ");
- str.concat("(" + Integer.toString(clients[i].id) + "," + Integer.toString(clients[i].tArr) + ","
- + Integer.toString(clients[i].tServ) + "); ");
- }
- // System.out.println();
- str.concat("'\n'");
- for (int i = 0; i < maxQueues; ++i) {
- //System.out.print("Queue " + Integer.toString(i + 1) + ": ");
- str.concat("Queue " + Integer.toString(i + 1) + ": ");
- if (queues[i].size() == 0)
- //System.out.print("closed");
- str.concat("closed");
- else {
- for (int j = 0; j < queues[i].size(); ++j)
- //System.out.print(
- // "(" + Integer.toString(queues[i].get(j).id) + "," + Integer.toString(queues[i].get(j).tArr)
- // + "," + Integer.toString(queues[i].get(j).tServ) + "); ");
- str.concat(
- "(" + Integer.toString(queues[i].get(j).id) + "," + Integer.toString(queues[i].get(j).tArr)
- + "," + Integer.toString(queues[i].get(j).tServ) + "); ");
- //System.out.println();
- str.concat("'\n'");
- }
- }
- //System.out.println();
- str.concat("'\n'");
- return str;
- }
- }
- class Client {
- int id;
- int tArr;
- int tServ;
- public Client(final int _id, final int _tArr, final int _tServ) {
- id = _id;
- tArr = _tArr;
- tServ = _tServ;
- }
- }
- class MainClass {
- public static void main(final String args[]) {
- final File in = new File("input.txt");
- try {
- final Scanner sc = new Scanner(in);
- int n, q, tMax, minArr, maxArr, minServ, maxServ;
- n = sc.nextInt();
- q = sc.nextInt();
- tMax = sc.nextInt();
- String line = sc.nextLine();
- line = sc.nextLine();
- String[] lineVector = line.split(",");
- minArr = Integer.parseInt(lineVector[0]);
- maxArr = Integer.parseInt(lineVector[1]);
- line = sc.nextLine();
- lineVector = line.split(",");
- minServ = Integer.parseInt(lineVector[0]);
- maxServ = Integer.parseInt(lineVector[1]);
- sc.close();
- final Client clients[] = new Client[1000];
- final Random rng = new Random();
- for (int i = 0; i < n; ++i) {
- final int tA = minArr + rng.nextInt((int) (maxArr - minArr + 1));
- final int tS = minServ + rng.nextInt((int) (maxServ - minServ + 1));
- clients[i] = (new Client(i, tA, tS));
- }
- final Manager man = new Manager(clients, n, tMax, q);
- (new Thread(man)).start();
- } catch (final FileNotFoundException e)
- {
- System.out.println("Input file not found");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement