Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SingleQueue {
- final static int arrivalTimeLimit = 3600; // set the arrival time limit in seconds
- final static double arrivalProbability = 1.0/10; // Probability a request will arrive at any given second
- final static int numberOfServers = 5; // set the number of servers available to process the queue
- public static void main(String[] args) {
- Queue<Request> queue = new Queue<Request>();
- Random checkArrival = new Random();
- int totalWaitTime = 0;
- int totalServed = 0;
- int [] serverArray = new int[numberOfServers];
- //
- // In the simulation, we simulate each second of time. We continue the simulation until we reach
- // the time limit and the queue is empty.
- //
- for (int currentSecond = 1; currentSecond <= arrivalTimeLimit || queue.size() > 0; currentSecond++) {
- // System.out.println(currentSecond);
- //
- // If the number of seconds has not exceeded the time after which no one is allowed to queue up,
- // check whether someone has arrived. If so, put their arrival time on the queue.
- //
- if (currentSecond <= arrivalTimeLimit) {
- boolean arrivalHappened = (checkArrival.nextDouble() <= arrivalProbability);
- if (arrivalHappened) {
- Random setTimeToServe = new Random(); // creates a random object to generate a random timeToServe for each request
- int timeToServe = setTimeToServe.nextInt(49) + 10;
- Request newRequest = new Request(currentSecond, timeToServe);
- queue.enqueue(newRequest);
- }
- }
- for (int server = 0; server < numberOfServers; server++){
- //
- // If someone is currently being served, decrement the time remaining to serve by 1.
- // Do this if server is serving
- if (serverArray[server] > 0) {
- serverArray[server]--;
- }
- //
- // Otherwise no one is being served. Check whether anyone is waiting on the queue and,
- // if so, begin serving them. Add their wait to time to the total wait time and increment
- // the number served.
- //
- else if (queue.size() > 0) {
- Request processRequest = queue.dequeue();
- serverArray[server] = processRequest.serviceTime;
- int arrivalTime = processRequest.arrivalTime;
- int waitTime = currentSecond - arrivalTime;
- totalWaitTime += waitTime;
- totalServed++;
- }
- }
- }
- System.out.println("Number served: " + totalServed);
- System.out.println("Total wait time: " + totalWaitTime);
- System.out.println("Average wait time: " + (double)totalWaitTime/totalServed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement