1 /******************************************************************************2 * Compilation:javac Mm1queue.java3 * Execution:java mm1queue Lambda mu4 * Dependencies:Queue.java Histogram.java5 *6 * Simulate an M/M/1 queue where arrivals and departures is Poisson7 * Processes with arrival rate lambda and service rate mu.8 *9 *% java mm1queue.Ten * One *% java mm1queue. A * - *% java mm1queue. - * the * - * Remarks - * ------- - *-We assume the interrarrival and service times are independent. + * - * + ******************************************************************************/ A at Public classMm1queue { - - Public Static voidMain (string[] args) { - DoubleLambda = double.parsedouble (args[0]);//Arrival Rate - Doublemu = double.parsedouble (args[1]);//Service Rate - inqueue<double> q =NewQueue<double> ();//arrival Times of customers - DoubleNextarrival = Stdrandom.exp (lambda);//Time of next arrival to DoubleNextdeparture = double.positive_infinity;//Time of next departure + - //Histogram Object theHistogram hist =NewHistogram (60); * $ //simulate an M/M/1 queuePanax Notoginseng while(true) { - the //it ' s an arrival + if(Nextarrival <=nextdeparture) { A if(Q.isempty ()) Nextdeparture = Nextarrival +stdrandom.exp (MU); the Q.enqueue (nextarrival); +Nextarrival + =stdrandom.exp (lambda); - } $ $ //it ' s a departure - Else { - Doublewait = nextdeparture-q.dequeue (); thestdout.printf ("Wait =%6.2f, Queue size =%d\n", Wait, q.size ()); -Hist.adddatapoint (Math.min (60, (int) (Math.Round (Wait) ));Wuyi Hist.draw (); the if(Q.isempty ()) Nextdeparture =double.positive_infinity; - ElseNextdeparture + =stdrandom.exp (MU); Wu - } About } $ - } - -}
Algorithm Sedgewick Fourth Edition-1th Chapter Foundation -024-M/M/1 Queue