All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rpc.turbo.loadbalance.RandomLoadBalance Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package rpc.turbo.loadbalance;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 速度略慢 115.069 ± 1.193 ops/us
 * 
 * @author Hank
 *
 * @param 
 *            必须为Weightable子类
 */
public class RandomLoadBalance implements LoadBalance {

	protected volatile WeightableGroup weightableGroup = null;

	@Override
	public void setWeightables(List weightables) {
		weightableGroup = new WeightableGroup<>(weightables);
	}

	@Override
	public T select() {
		if (weightableGroup == null) {
			return null;
		}

		int sum = weightableGroup.sum();

		if (sum < 2) {
			return weightableGroup.get(0);
		}

		int seed = ThreadLocalRandom.current().nextInt(sum);
		return weightableGroup.get(seed);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy