【问题标题】:Simulating a single queue with multiple servers in Java在 Java 中模拟具有多个服务器的单个队列
【发布时间】: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);
}

}

有人可以就我如何修改它以适应四台服务器提供一些建议吗?提前致谢。

【问题讨论】:

    标签: java arrays queue


    【解决方案1】:

    我认为您可以将 int 变量 timeLeftToServe 替换为 4 个 int 的数组。这是代码:

    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);
        }
    
    }
    

    祝你有美好的一天。

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2016-12-06
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2021-03-27
      • 2020-11-23
      相关资源
      最近更新 更多