三队列排队论仿真(Java)
2017-11-21 00:42
393 查看
计算机网络相关,设计一个三队列一服务站的模型,三队列发包符合泊松分布,服务站对每个包有一定的服务速率,包的大小也符合泊松分布。
问题分析:如果采用实时模型,计算机内部的调度算法会占用一部分时间,导致实验结果不准确,因此采用时间轴的方法来模拟实时的发包和处理包的过程。
首先拟采用带赤字的时间片模型,三个基站持续按照泊松分布向三个队列发包,服务器采用一定的时间片,对三个队列进行轮询,每次对一个队列的服务时间不超过一个时间片。
实现方法:首先创建三个队列,运行生产(发包)程序,在队列中存储一定数量的pack对象,此时按照泊松分布为每个pack赋予到达时间属性。 然后运行消费(服务)程序,计算并且输出结果,在运行过程中,消费(服务)程序维护一个时间轴变量,以对pakc的时间属性进行判断(这就是问题分析中描述的时间轴方法)。
代码如下:(为防止同志们不自己思考直接copy,包含一些小错误,不过能运行)
pack:
public class pack {
public int index;
public double comeTime;
public double leafTime;
public double servTime;
public double waitTime;
public pack(int index) {
this.index = index;
}
public void CalLeaf(double tt) {
waitTime = (tt - comeTime); //暂定为这样把,实际应该传入
}
}Prod:
import java.util.ArrayList;
public class Prod {
int index;
double lamda;
ArrayList<pack> queue;
int length;
double currentTime; //用于记录当前时间线
public ArrayList<Integer> lengthDis;
public Prod(double lamda) {
this.lamda = lamda;
queue = new ArrayList<pack>();
lengthDis = new ArrayList<Integer>();
}
public Prod(int index,double lamda) {
this.index = index;
this.lamda = lamda;
queue = new ArrayList<pack>();
lengthDis = new ArrayList<Integer>();
}
public void getLength() {
length = queue.size();
}
public void Run(int count) {
for(int i=0;i<count;i++) {
//_______用于调试
System.out.println("队列"+index+" 包"+i);
//-------------
pack temppack = new pack(i); //生成新的包
temppack.comeTime = currentTime; //设置包的到来时间为当前时间
double comeInter = -Math.log(Math.random())/lamda; //测试阶段设置整形
currentTime += comeInter; //生产者计时器应该
queue.add(temppack); //向队列中添加
}
}
}
Cons:
生成数据后采用matlab画图就可生成图像了。
问题分析:如果采用实时模型,计算机内部的调度算法会占用一部分时间,导致实验结果不准确,因此采用时间轴的方法来模拟实时的发包和处理包的过程。
首先拟采用带赤字的时间片模型,三个基站持续按照泊松分布向三个队列发包,服务器采用一定的时间片,对三个队列进行轮询,每次对一个队列的服务时间不超过一个时间片。
实现方法:首先创建三个队列,运行生产(发包)程序,在队列中存储一定数量的pack对象,此时按照泊松分布为每个pack赋予到达时间属性。 然后运行消费(服务)程序,计算并且输出结果,在运行过程中,消费(服务)程序维护一个时间轴变量,以对pakc的时间属性进行判断(这就是问题分析中描述的时间轴方法)。
代码如下:(为防止同志们不自己思考直接copy,包含一些小错误,不过能运行)
pack:
public class pack {
public int index;
public double comeTime;
public double leafTime;
public double servTime;
public double waitTime;
public pack(int index) {
this.index = index;
}
public void CalLeaf(double tt) {
waitTime = (tt - comeTime); //暂定为这样把,实际应该传入
}
}Prod:
import java.util.ArrayList;
public class Prod {
int index;
double lamda;
ArrayList<pack> queue;
int length;
double currentTime; //用于记录当前时间线
public ArrayList<Integer> lengthDis;
public Prod(double lamda) {
this.lamda = lamda;
queue = new ArrayList<pack>();
lengthDis = new ArrayList<Integer>();
}
public Prod(int index,double lamda) {
this.index = index;
this.lamda = lamda;
queue = new ArrayList<pack>();
lengthDis = new ArrayList<Integer>();
}
public void getLength() {
length = queue.size();
}
public void Run(int count) {
for(int i=0;i<count;i++) {
//_______用于调试
System.out.println("队列"+index+" 包"+i);
//-------------
pack temppack = new pack(i); //生成新的包
temppack.comeTime = currentTime; //设置包的到来时间为当前时间
double comeInter = -Math.log(Math.random())/lamda; //测试阶段设置整形
currentTime += comeInter; //生产者计时器应该
queue.add(temppack); //向队列中添加
}
}
}
Cons:
import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; public class Cons { public double currentTime; public double Mu; public double timePiece; public String str = "E://output3.txt"; public static void write(String fileName, String content) { FileWriter writer = null; try { // 打开一个写文件器,构造 4000 函数中的第二个参数true表示以追加形式写文件 writer = new FileWriter(fileName, true); writer.write(content); } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null){ writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String args[]) { Cons cons = new Cons(2); } public Cons(double Mu) { this.Mu = Mu; } public Cons(double Mu,double timePiece) { this.Mu = Mu; this.timePiece = timePiece; } public void Consume(Prod producer) { //单队列的服务方法 while(!producer.queue.isEmpty()) { if(currentTime >= producer.queue.get(0).comeTime) { pack temp = producer.queue.get(0); temp.CalLeaf(currentTime); temp.servTime = -Math.log(Math.random())/Mu; currentTime += temp.servTime; //输出包的信息 } else { currentTime += 0.01; } } } public int CalMeanQueueLength(Prod producer) { //计算平均队列长度 int index = 0; while(!producer.queue.isEmpty()&&index<producer.queue.size()&&producer.queue.get(index).comeTime<=currentTime) { index++; } producer.lengthDis.add(index+1); return index+1; } public void ConsTimePiece(Prod producer) throws IOException { //服务一个时间片的方法 double serve = 0; int QueueLength = 0; while((!producer.queue.isEmpty())&&serve<timePiece) { if(producer.queue.get(0).comeTime<=currentTime) { // hahaha = 0; pack temp = producer.queue.get(0); temp.CalLeaf(currentTime); temp.servTime = -Math.log(Math.random())/Mu; currentTime += temp.servTime; serve += temp.servTime; //输出包的信息 // HSSFRow rows = sheet.createRow(row); // row ++; // System.out.println("line:"+ producer.index +"pack:"+temp.index); // rows.createCell((short) 0).setCellValue(producer.index); // rows.createCell((short) 1).setCellValue(temp.index); // // rows.createCell((short) 3).setCellValue(temp.waitTime); // QueueLength = CalMeanQueueLength(producer); write(str,producer.index+" "+temp.index+" "+temp.waitTime+" "+QueueLength +"\r\n"); System.out.print("队列"+producer.index+" 包: "+temp.index+" " + "waittime: "+temp.waitTime); producer.queue.remove(0); System.out.println(); }else { currentTime += 0.01; // hahaha++; // System.out.println("hahaha"); break; } } } public int hahaha; public void Consume3(Prod producer1,Prod producer2, Prod producer3) throws IOException{ //三队列的服务方法 //此方法为带赤字的时间片调度算法,对三个生产者产生的包文件进行调度 // 调度算法为:对三个生产者队列进行轮询,当有符合要求的文件时(Prod.time.get(0)<=currentTime) // 在不超过时间片长度的情况下可以一直对该生产者队列服务,但是一旦超过了时间片长度则轮询下个队列 // 并且允许且只允许一个包的服务时间超过时间片 //---------------------------------------------------------------- int n = 1; while(!producer1.queue.isEmpty()||!producer2.queue.isEmpty()||!producer3.queue.isEmpty()) { if(n==1) ConsTimePiece(producer1); else if(n==2) ConsTimePiece(producer2); else ConsTimePiece(producer3); // if(hahaha>=3) { // if(!producer1.queue.isEmpty()&&!producer2.queue.isEmpty()&&!producer3.queue.isEmpty()) // currentTime = min3(producer1.queue.get(0).comeTime, producer2.queue.get(0).comeTime, producer3.queue.get(0).comeTime); // if(!producer1.queue.isEmpty()&&!producer2.queue.isEmpty()&&producer3.queue.isEmpty()) // currentTime = min(producer1.queue.get(0).comeTime, producer2.queue.get(0).comeTime); // if(!producer1.queue.isEmpty()&&producer2.queue.isEmpty()&&!producer3.queue.isEmpty()) // currentTime = min(producer1.queue.get(0).comeTime, producer3.queue.get(0).comeTime); // if(producer1.queue.isEmpty()&&!producer2.queue.isEmpty()&&!producer3.queue.isEmpty()) // currentTime = min(producer2.queue.get(0).comeTime, producer3.queue.get(0).comeTime); // if(!producer1.queue.isEmpty()&&producer2.queue.isEmpty()&&producer3.queue.isEmpty()) // currentTime = producer1.queue.get(0).comeTime; // if(!producer2.queue.isEmpty()&&producer1.queue.isEmpty()&&producer3.queue.isEmpty()) // currentTime = producer2.queue.get(0).comeTime; // if(!producer3.queue.isEmpty()&&producer1.queue.isEmpty()&&producer2.queue.isEmpty()) // currentTime = producer3.queue.get(0).comeTime; // } // else { // 改变n值 if(n==1) n=2; else if(n==2) n=3; else n=1; // } } } public double min(double a,double b) { return (a<b)?a:b; } public double min3(double a,double b,double c) { return (min(a,b)>c)?(min(a,b)):c; } }run:
import java.io.IOException; import java.util.ArrayList; public class run { public static int count; //整型足以描述10^8 public static void main(String args[]) throws IOException{ Prod p1 = new Prod(1,3); Prod p2 = new Prod(2,4); Prod p3 = new Prod(3,5); p1.Run(1000000); p2.Run(1000000); p3.Run(1000000); Cons c = new Cons(10,4); c.Consume3(p1,p2,p3); } }
生成数据后采用matlab画图就可生成图像了。
相关文章推荐
- java中线程队列BlockingQueue的用法
- Java消息队列--ActiveMq 实战
- java 实现数据结构之队列
- 【死磕Java并发】-----J.U.C之AQS:CLH同步队列
- java多线程和队列实例
- 数据结构与算法之三(栈和队列的java实现)
- java 阻塞队列
- 18-java5阻塞队列实现线程间通信-实现线程间通信方式(3)
- 疯狂java笔记之栈和队列
- JavaWeb项目架构之Kafka分布式日志队列
- Java里的阻塞队列
- java使用闭环数组自定义高性能队列
- 详解Java消息队列-Spring整合ActiveMq
- 从头认识java-17.5 阻塞队列(以生产者消费者模式为例)
- Java实现队列
- JAVA实现队列的顺序存储结构和操作
- 到底什么是消息队列?Java中如何实现消息队列?
- java中使用队列:java.util.Queue
- Java 用两个队列实现一个栈,要求top()的时间复杂度为O(1)
- Java数据结构与算法(4) - ch04队列(Queue和PriorityQ)