Java 退避算法仿真
我正在尝试模拟(仅模拟!!)退避算法,但我不知道我的路径是否正确 无论如何,它是这样的:Java 退避算法仿真,java,Java,我正在尝试模拟(仅模拟!!)退避算法,但我不知道我的路径是否正确 无论如何,它是这样的: 每个线程都有随机的时间段 每个线程减少其时间段直到0 第一个线程获得的时间槽=0将读取全局变量 读取后,如果全局变量为0(0表示线程可以执行重传,否则如果为1则没有线程可以执行重传),则第一个线程将全局变量设置为1,然后执行重传(在本例中,只需在屏幕上打印一些内容) 在第一个线程完成后,他将全局变量设置为0(以便下一个线程可以进行重传) 等等 那么,我可以使用分布式系统(套接字TCP/UDP)实现这个场景吗
/**
* canal is global variable . if 0 means can the thread can do the
* retransmission else if its 1 then no thread can do retransmission
*/
public int canal = 0;
/**
* Each thread got timeSlot that can take random value between two numbers 1 and 12 .
* timeSlot value start decreasing until 0.
*/
public void backOff() {
int timeSlot = 1 + (int) (Math.random() * ((12 - 1) + 1));
for (int i = timeSlot; i <= 0; i--) {
timeSlot--;
System.out.println("timeSlot number :" + timeSlot);
}
if (canal == 0) {
canal = 1;
System.out.println("retransmission)");
canal = 0;
}
}
/**
*canal是全局变量。如果0表示线程可以执行
*重传否则如果为1,则没有线程可以进行重传
*/
公共int运河=0;
/**
*每个线程都有一个时隙,它可以取两个数字1和12之间的随机值。
*时间段值开始减小,直到0。
*/
公共无效回退(){
int时隙=1+(int)(Math.random()*((12-1)+1));
对于(int i=timeSlot;我想说如果这是一个模拟:多线程。好的,多线程,那么,我希望你能帮助我如何开始使用代码!。感谢你最好先阅读一些多线程…网上有很多文章。