Java Reference
In-Depth Information
1 /**
2 * Constructor.
3 * @param operator number of operators.
4 * @param avgLen averge length of a call.
5 * @param callIntrvl the average time between calls.
6 */
7 public CallSim( int operators, double avgLen, int callIntrvl )
8 {
9 eventSet = new PriorityQueue<Event>( );
10 availableOperators = operators;
11 avgCallLen = avgLen;
12 freqOfCalls = callIntrvl;
13 r = new Random( );
14 nextCall( freqOfCalls ); // Schedule first call
15 }
figure 13.7
The CallSim
constructor
1 private int userNum = 0;
2 private int nextCallTime = 0;
3
4 /**
5 * Place a new DIAL_IN event into the event queue.
6 * Then advance the time when next DIAL_IN event will occur.
7 * In practice, we would use a random number to set the time.
8 */
9 private void nextCall( int delta )
10 {
11 Event ev = new Event( userNum++, nextCallTime, Event.DIAL_IN );
12 eventSet.add( ev );
13 nextCallTime += delta;
14 }
figure 13.8
The nextCall method
places a new DIAL_IN
event in the event
queue and advances
the time when the
next DIAL_IN event will
occur
The other method is runSim , which is called to run the entire simulation.
The runSim method does most of the work and is shown in Figure 13.9. It is
called with a single parameter that indicates when the simulation should end.
As long as the event set is not empty, we process events. Note that it should
never be empty because at the time we arrive at line 12 there is exactly one
dial-in request in the priority queue and one hang-up request for every cur-
rently connected caller. Whenever we remove an event at line 12 and it is con-
firmed to be a dial-in, we generate a replacement dial-in event at line 40. A
hang-up event is also generated at line 35 if the dial-in succeeds. Thus the
only way to finish the routine is if nextCall is set up not to generate an event
eventually or (more likely) by executing the break statement at line 15.
The runSim method
runs the simulation.
 
Search WWH ::




Custom Search