算法Sedgewick第四版-第1章基础-024-M/M/1 queue
2016-04-20 11:50
363 查看
/****************************************************************************** * Compilation: javac MM1Queue.java * Execution: java MM1Queue lambda mu * Dependencies: Queue.java Histogram.java * * Simulate an M/M/1 queue where arrivals and departures are Poisson * processes with arrival rate lambda and service rate mu. * * % java MM1Queue .20 .33 * * % java MM1Queue .20 .25 * * % java MM1Queue .20 .21 * * * Remarks * ------- * - We assume the interrarrival and service times are independent. * * ******************************************************************************/ public class MM1Queue { public static void main(String[] args) { double lambda = Double.parseDouble(args[0]); // arrival rate double mu = Double.parseDouble(args[1]); // service rate Queue<Double> q = new Queue<Double>(); // arrival times of customers double nextArrival = StdRandom.exp(lambda); // time of next arrival double nextDeparture = Double.POSITIVE_INFINITY; // time of next departure // histogram object Histogram hist = new Histogram(60); // simulate an M/M/1 queue while (true) { // it's an arrival if (nextArrival <= nextDeparture) { if (q.isEmpty()) nextDeparture = nextArrival + StdRandom.exp(mu); q.enqueue(nextArrival); nextArrival += StdRandom.exp(lambda); } // it's a departure else { double wait = nextDeparture - q.dequeue(); StdOut.printf("Wait = %6.2f, queue size = %d\n", wait, q.size()); hist.addDataPoint(Math.min(60, (int) (Math.round(wait)))); hist.draw(); if (q.isEmpty()) nextDeparture = Double.POSITIVE_INFINITY; else nextDeparture += StdRandom.exp(mu); } } } }
相关文章推荐
- HDU4027(Can you answer these queries?)
- 自定义UITableViewCell的两种方式
- iOS开发调整UILabel的行间距
- 【UE4学习】06——蓝图常用函数
- 算法Sedgewick第四版-第1章基础-022一QueueWithTwoStacks
- 编译原理:用bison实现huiwen.y和huiwen.lex判断是否为回文
- java序列化和serialVersionUID
- String、stringbuffer与stringbuilder的区别
- UGUI实现的虚拟摇杆,可改变摇杆位置
- BlockingQueue的使用
- [Form Builder]POST 与 commit_form 的区别
- iOS7之后,关于NavigationBar存在时,UIScrollView内容发生偏移的问题
- Android 版本与 Bluetooth 版本之间的关系
- WPF:MenuItem样式
- 还来一篇说下json_value 以及 json_query 的应用 (3)
- edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets属性详解 )——转载
- SPOJ QTREE- Query on a tree (树链剖分)
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- 如何写好一个UITableView(上)
- UITabBarController自定义转场动画