带权重的轮询算法 java实现_权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现2...

1 importjava.util.ArrayList;2 importjava.util.Date;3 importjava.util.HashMap;4 importjava.util.List;5 importjava.util.Map;6 importjava.util.Map.Entry;7

8 /**

9 * 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现10 *@authorhuligong11 **/

12 public classWeightedRoundRobinScheduling {13

14 private List serverList; //服务器集合

15

16 publicServer GetBestServer() {17 Server server = null;18 Server best = null;19 int total = 0;20 for(int i=0,len=serverList.size();i

22 server =serverList.get(i);23

24 //当前服务器已宕机,排除

25 if(server.down){26 continue;27 }28

29 server.currentWeight +=server.effectiveWeight;30 total +=server.effectiveWeight;31

32 if(server.effectiveWeight

36 if(best == null || server.currentWeight>best.currentWeight){37 best =server;38 }39

40 }41

42 if (best == null) {43 return null;44 }45

46 best.currentWeight -=total;47 best.checkedDate = newDate();48 returnbest;49 }50

51

52

53 classServer {54 publicString ip;55 public intweight;56 public inteffectiveWeight;57 public intcurrentWeight;58 public boolean down = false;59 publicDate checkedDate;60 public Server(String ip, intweight) {61 super();62 this.ip =ip;63 this.weight =weight;64 this.effectiveWeight = this.weight;65 this.currentWeight = 0;66 if(this.weight < 0){67 this.down = true;68 }else{69 this.down = false;70 }71 }72 publicString getIp() {73 returnip;74 }75 public voidsetIp(String ip) {76 this.ip =ip;77 }78 public intgetWeight() {79 returnweight;80 }81 public void setWeight(intweight) {82 this.weight =weight;83 }84 public intgetEffectiveWeight() {85 returneffectiveWeight;86 }87 public void setEffectiveWeight(inteffectiveWeight) {88 this.effectiveWeight =effectiveWeight;89 }90 public intgetCurrentWeight() {91 returncurrentWeight;92 }93 public void setCurrentWeight(intcurrentWeight) {94 this.currentWeight =currentWeight;95 }96 public booleanisDown() {97 returndown;98 }99 public void setDown(booleandown) {100 this.down =down;101 }102 publicDate getCheckedDate() {103 returncheckedDate;104 }105 public voidsetCheckedDate(Date checkedDate) {106 this.checkedDate =checkedDate;107 }108

109 }110

111

112 public voidinit() {113 Server s1 = new Server("192.168.0.100", 3);//3

114 Server s2 = new Server("192.168.0.101", 2);//2

115 Server s3 = new Server("192.168.0.102", 6);//6

116 Server s4 = new Server("192.168.0.103", 4);//4

117 Server s5 = new Server("192.168.0.104", 1);//1

118 Server s6 = new Server("192.168.0.105", 0);//0

119 Server s7 = new Server("192.168.0.106", 0);//0

120 Server s8 = new Server("192.168.0.107", 0);//0

121 Server s9 = new Server("192.168.0.108", 0);//0

122 serverList = new ArrayList();123 serverList.add(s1);124 serverList.add(s2);125 serverList.add(s3);126 serverList.add(s4);127 serverList.add(s5);128 serverList.add(s6);129 serverList.add(s7);130 serverList.add(s8);131 serverList.add(s9);132 }133

134 public void add(inti) {135 Server s = new Server("192.168.0.1"+i, i-15);136 serverList.add(s);137 }138

139 public Server getServer(inti) {140 if(i

146

147 public static voidmain(String[] args) {148 WeightedRoundRobinScheduling obj = newWeightedRoundRobinScheduling();149 obj.init();150

151 Map countResult = new HashMap();152

153 for (int i = 0; i < 100; i++) {154 Server s =obj.GetBestServer();155 String log = "ip:"+s.ip+";weight:"+s.weight;156 if(countResult.containsKey(log)){157 countResult.put(log,countResult.get(log)+1);158 }else{159 countResult.put(log,1);160 }161 System.out.println(log);162

163 //动态添加服务器

164 if(i==20 || i==22){165 obj.add(i);166 }167

168 //动态停止服务器

169 if(i==30){170 obj.getServer(2).setDown(true);171 obj.getServer(3).setDown(true);172 }173 }174

175 for(Entrymap : countResult.entrySet()){176 System.out.println("服务器 "+map.getKey()+" 请求次数: "+map.getValue());177 }178 }179

180 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值