我正在尝试修改一个程序,该程序将单个服务器所服务的单个队列模拟为由四个服务器提供服务的单个队列。以下是单服务器情况的代码:
public class SimulateSQMS {
final static int arrivalTimeLimit = 3600;
final static double arrivalProbability = 5.0/100;
final static int timeToServe = 15;
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>();
Random checkArrival = new Random();
int timeLeftToServe = 0;
int totalWaitTime = 0;
int totalServed = 0;
//
// 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++) {
//
// 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) {
queue.enqueue(currentSecond);
}
}
//
// If someone is currently being served, decrement the time remaining to serve by 1.
//
if (timeLeftToServe > 0) {
timeLeftToServe--;
}
//
// 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) {
int arrivalTime = queue.dequeue();
int waitTime = currentSecond - arrivalTime;
totalWaitTime += waitTime;
totalServed++;
timeLeftToServe = timeToServe;
}
}
StdOut.println("Number served: " + totalServed);
StdOut.println("Total wait time: " + totalWaitTime);
StdOut.println("Average wait time: " + (double)totalWaitTime/totalServed);
}
}
有人可以提供一些建议,告诉我如何修改这个来计算四台服务器吗?提前谢谢。
答案 0 :(得分:0)
我认为你可以用4 int的数组替换你的int变量timeLeftToServe。这是代码:
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Random;
public class SimulateSQMS {
final static int arrivalTimeLimit = 3600;
final static double arrivalProbability = 5.0 / 100;
final static int timeToServe = 15;
public static void main(String[] args) {
Deque<Integer> queue = new ArrayDeque<Integer>();
Random checkArrival = new Random();
int[] timeLeftToServe = new int[4];
int totalWaitTime = 0;
int totalServed = 0;
//
// 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++) {
//
// 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) {
queue.add(currentSecond);
}
}
//
// If someone is currently being served, decrement the time
// remaining to serve by 1.
//
for (int i = 0; i < 4; ++i) {
if (timeLeftToServe[i] > 0) {
timeLeftToServe[i]--;
}
//
// 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) {
int arrivalTime = queue.poll();
int waitTime = currentSecond - arrivalTime;
totalWaitTime += waitTime;
totalServed++;
timeLeftToServe[i] = timeToServe;
}
}
}
System.out.println("Number served: " + totalServed);
System.out.println("Total wait time: " + totalWaitTime);
System.out.println("Average wait time: " + (double) totalWaitTime / totalServed);
}
}
度过美好的一天。