Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

SingleQueue

By: a guest on Oct 10th, 2012  |  syntax: Java  |  size: 2.52 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.*;
  2.  
  3.  
  4. public class SingleQueue {
  5.        
  6.         final static int arrivalTimeLimit = 3600; // set the arrival time limit in seconds
  7.         final static double arrivalProbability = 1.0/10; // Probability a request will arrive at any given second
  8.         final static int numberOfServers = 5; // set the number of servers available to process the queue
  9.        
  10.  
  11.         public static void main(String[] args) {
  12.                 Queue<Request> queue = new Queue<Request>();
  13.                 Random checkArrival = new Random();
  14.                 int totalWaitTime = 0;
  15.                 int totalServed = 0;
  16.                 int [] serverArray = new int[numberOfServers];
  17.  
  18.                 //
  19.                 // In the simulation, we simulate each second of time.  We continue the simulation until we reach
  20.                 // the time limit and the queue is empty.
  21.                 //
  22.                 for (int currentSecond = 1; currentSecond <= arrivalTimeLimit || queue.size() > 0; currentSecond++) {
  23.                         // System.out.println(currentSecond);
  24.                         //
  25.                         // If the number of seconds has not exceeded the time after which no one is allowed to queue up,
  26.                         // check whether someone has arrived.  If so, put their arrival time on the queue.
  27.                         //
  28.                         if (currentSecond <= arrivalTimeLimit) {
  29.                                 boolean arrivalHappened = (checkArrival.nextDouble() <= arrivalProbability);
  30.                                 if (arrivalHappened) {
  31.                                         Random setTimeToServe = new Random(); // creates a random object to generate a random timeToServe for each request
  32.                                         int timeToServe = setTimeToServe.nextInt(49) + 10;
  33.                                         Request newRequest = new Request(currentSecond, timeToServe);
  34.                                         queue.enqueue(newRequest);                                     
  35.                                 }
  36.                         }
  37.                        
  38.                         for (int server = 0; server < numberOfServers; server++){
  39.                                 //
  40.                                 // If someone is currently being served, decrement the time remaining to serve by 1.
  41.                                 // Do this if server is serving
  42.                                 if (serverArray[server] > 0) {
  43.                                         serverArray[server]--;
  44.                                 }
  45.                                 //
  46.                                 // Otherwise no one is being served.  Check whether anyone is waiting on the queue and,
  47.                                 // if so, begin serving them.  Add their wait to time to the total wait time and increment
  48.                                 // the number served.
  49.                                 //
  50.                                 else if (queue.size() > 0) {
  51.                                         Request processRequest = queue.dequeue();
  52.                                         serverArray[server] = processRequest.serviceTime;
  53.                                         int arrivalTime = processRequest.arrivalTime;
  54.                                         int waitTime = currentSecond - arrivalTime;
  55.                                         totalWaitTime += waitTime;
  56.                                         totalServed++;
  57.                                
  58.                                 }
  59.                         }
  60.                 }
  61.                 System.out.println("Number served: " + totalServed);
  62.                 System.out.println("Total wait time: " + totalWaitTime);
  63.                 System.out.println("Average wait time: " + (double)totalWaitTime/totalServed);
  64.                
  65.         }
  66. }