【发布时间】:2015-03-12 21:09:39
【问题描述】:
我正在尝试修改一个程序,该程序模拟由单个服务器提供服务的单个队列为由四个服务器提供服务的单个队列。下面是单服务器情况的代码:
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);
}
}
有人可以就我如何修改它以适应四台服务器提供一些建议吗?提前致谢。
【问题讨论】: