给定一个权重数组,实现一个带权重的路由策略

2021-04-11 17:26

阅读:345

标签:public   new   round   static   实现   tin   说明   get   路由   

说明:服务的索引就是权重数组的下标

import java.util.*;

public class Client {

    public static void main(String[] args) {
        int[] arr = {3,4,5,6,7};

        List weights = new ArrayList();
        int index = 0;
        int sum = 0;
        for (int i = 0; i ) {
            Weight weight = new Weight();
            weight.setStart(index);
            weight.setEnd(index + arr[i]);
            weight.setServer(i);
            index += arr[i];
            weights.add(weight);
            sum += arr[i];
        }
        Random random = new Random();

        for (int i = 0; i ) {
            int round = random.nextInt(sum);

            for (Weight weight : weights) {
                if(round >= weight.getStart() && round  weight.getEnd()){
                    System.out.println("round:"+round+",server:"+weight.getServer()+", start:"+weight.getStart()+",end:"+weight.getEnd());
                }
            }
        }


    }

}

 

public class Weight {
    private int start;
    private int end;
    private int server;

 

给定一个权重数组,实现一个带权重的路由策略

标签:public   new   round   static   实现   tin   说明   get   路由   

原文地址:https://www.cnblogs.com/dongma/p/13358062.html

上一篇:数组VS集合

下一篇:3. python运算符


评论


亲,登录后才可以留言!