仿摄影网站,企业网站建设源码 微信 手机,简述三只松鼠网络营销方式,做自己的视频网站转载自 Round Robin 算法
什么是Round Robin#xff1f;
先来看和他相近的名词#xff0c;轮询调度算法(Round-Robin Scheduling)
轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器#xff0c;从1开始#xff0c;直到N(内部服务器个数)#xff0…转载自 Round Robin 算法
什么是Round Robin
先来看和他相近的名词轮询调度算法(Round-Robin Scheduling)
轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器从1开始直到N(内部服务器个数)然后重新开始循环。
算法的优点是其简洁性它无需记录当前所有连接的状态所以它是一种无状态调度。
轮询调度算法流 假设有一组服务器N台S {S1, S2, …, Sn}一个指示变量i表示上一次选择的服务器ID。变量 i 被初始化为N-1。其算法如下
import java.util.concurrent.atomic.AtomicInteger;public class RoundRobin2 {/*** 线程安全的*/private final static AtomicInteger next new AtomicInteger(0);private int select(int S[]) throws Exception {if (S null || S.length 0)throw new Exception(exception);else {return S[next.getAndIncrement() % S.length];}}public static void main(String args[]) throws Exception {int S[] {0, 1, 2, 3, 4};RoundRobin2 roundRobin2 new RoundRobin2();for (int i 0; i 10; i) {System.out.println(roundRobin2.select(S));}}
}轮询算法的缺点轮询调度算法假设所有服务器的处理性能都相同不关心每台服务器的当前连接数和响应速度。当请求服务间隔时间变化比较大时轮询调度算法容易导致服务器间的负载不平衡。
所以此种均衡算法适合于服务器组中的所有服务器都有相同的软硬件配置并且平均服务请求相对均衡的情况。