M/M/2 系统如何处理java 中模拟队列的阻塞时间?
How to proccess blocking time of a simulated queue in java for an M/M/2 system?
你们好,模拟M/M/1的队列阻塞时间我想出了这个解决方案,但不幸的是它不是面向对象的,问题是我想用[=23模拟它=] 系统,例如我用 19 初始化 lambda,用 20 初始化 mu 只是为了简化计算任何解决方案,提示,代码示例将不胜感激。
public class Main {
public static void main(String[] args) {
final int MAX_ENTITY = 100000;
final int SYSTEM_CAPACITY = 5;
final int BUSY = 1;
final int IDLE = 0;
double lambda = 19, mu = 20;
int blocked = 0;
int queue_length = 0;
int server_state = IDLE;
int entity = 0;
double next_av = getArivalRand(lambda);
double next_dp = next_av + getDeparturedRand(lambda);
while (entity <= MAX_ENTITY) {
//Arrival
if (next_av <= next_dp) {
entity++;
if (server_state == IDLE) {
server_state = BUSY;
} else if (queue_length < SYSTEM_CAPACITY - 1) {
queue_length++;
} else {
blocked++;
}
next_av += getArivalRand(lambda);
} // Departure
else if (queue_length > 0) {
queue_length--;
next_dp = next_dp + getDeparturedRand(mu);
} else {
server_state = IDLE;
next_dp = next_av + getDeparturedRand(mu);
}
}
System.out.println("Blocked Etity:" + blocked + "\n");
}
public static double getArivalRand(double lambda) {
return -1 / lambda * Math.log(1 - Math.random());
}
public static double getDeparturedRand(double mu) {
return -1 / mu * Math.log(1 - Math.random());
}
}
编辑:
如果你不了解队列理论,请检查here
天哪,你的代码需要认真重构才能实现 M/M/2
。
我创建了一个要点文件 here,我认为它实现了你想要的,
在要点文件中,我创建了一个 Dispatcher
class 用于平衡两个服务器中的两个队列,并且我还用两个种子模拟了它,它更面向对象的方法,
here is an example code from gist file which is for balancing load of
the tasks
if (server1.getQueueLength() < server2.getQueueLength())
currentServer = server1;
else if (server1.getQueueLength() > server2.getQueueLength())
currentServer = server2;
else if (currentServer == server1)
currentServer = server2;
else
currentServer = server1;
你们好,模拟M/M/1的队列阻塞时间我想出了这个解决方案,但不幸的是它不是面向对象的,问题是我想用[=23模拟它=] 系统,例如我用 19 初始化 lambda,用 20 初始化 mu 只是为了简化计算任何解决方案,提示,代码示例将不胜感激。
public class Main {
public static void main(String[] args) {
final int MAX_ENTITY = 100000;
final int SYSTEM_CAPACITY = 5;
final int BUSY = 1;
final int IDLE = 0;
double lambda = 19, mu = 20;
int blocked = 0;
int queue_length = 0;
int server_state = IDLE;
int entity = 0;
double next_av = getArivalRand(lambda);
double next_dp = next_av + getDeparturedRand(lambda);
while (entity <= MAX_ENTITY) {
//Arrival
if (next_av <= next_dp) {
entity++;
if (server_state == IDLE) {
server_state = BUSY;
} else if (queue_length < SYSTEM_CAPACITY - 1) {
queue_length++;
} else {
blocked++;
}
next_av += getArivalRand(lambda);
} // Departure
else if (queue_length > 0) {
queue_length--;
next_dp = next_dp + getDeparturedRand(mu);
} else {
server_state = IDLE;
next_dp = next_av + getDeparturedRand(mu);
}
}
System.out.println("Blocked Etity:" + blocked + "\n");
}
public static double getArivalRand(double lambda) {
return -1 / lambda * Math.log(1 - Math.random());
}
public static double getDeparturedRand(double mu) {
return -1 / mu * Math.log(1 - Math.random());
}
}
编辑:
如果你不了解队列理论,请检查here
天哪,你的代码需要认真重构才能实现 M/M/2
。
我创建了一个要点文件 here,我认为它实现了你想要的,
在要点文件中,我创建了一个 Dispatcher
class 用于平衡两个服务器中的两个队列,并且我还用两个种子模拟了它,它更面向对象的方法,
here is an example code from gist file which is for balancing load of the tasks
if (server1.getQueueLength() < server2.getQueueLength())
currentServer = server1;
else if (server1.getQueueLength() > server2.getQueueLength())
currentServer = server2;
else if (currentServer == server1)
currentServer = server2;
else
currentServer = server1;