mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!151 bug fix 随机权重并发问题
Merge pull request !151 from chengxian-yi/hutool-ycx
This commit is contained in:
commit
ec8926745d
@ -32,7 +32,7 @@ public class WeightRandom<T> implements Serializable {
|
|||||||
private static final long serialVersionUID = -8244697995702786499L;
|
private static final long serialVersionUID = -8244697995702786499L;
|
||||||
|
|
||||||
private final TreeMap<Double, T> weightMap;
|
private final TreeMap<Double, T> weightMap;
|
||||||
private final Random random;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建权重随机获取器
|
* 创建权重随机获取器
|
||||||
@ -50,7 +50,7 @@ public class WeightRandom<T> implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public WeightRandom() {
|
public WeightRandom() {
|
||||||
weightMap = new TreeMap<>();
|
weightMap = new TreeMap<>();
|
||||||
random = RandomUtil.getRandom();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,6 +141,7 @@ public class WeightRandom<T> implements Serializable {
|
|||||||
if(MapUtil.isEmpty(this.weightMap)) {
|
if(MapUtil.isEmpty(this.weightMap)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
final Random random = RandomUtil.getRandom();
|
||||||
final double randomWeight = this.weightMap.lastKey() * random.nextDouble();
|
final double randomWeight = this.weightMap.lastKey() * random.nextDouble();
|
||||||
final SortedMap<Double, T> tailMap = this.weightMap.tailMap(randomWeight, false);
|
final SortedMap<Double, T> tailMap = this.weightMap.tailMap(randomWeight, false);
|
||||||
return this.weightMap.get(tailMap.firstKey());
|
return this.weightMap.get(tailMap.firstKey());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user