Delegate to j.u.c.ThreadLocalRandom for Random instances

This commit is contained in:
Jason Tedor 2015-12-12 17:51:46 -05:00
parent 089a9e6977
commit 2602439a51
1 changed files with 2 additions and 17 deletions

View File

@ -22,10 +22,10 @@ package org.elasticsearch.common;
import org.elasticsearch.common.settings.Settings;
import java.lang.reflect.Method;
import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* Provides factory methods for producing reproducible sources of
@ -111,25 +111,10 @@ public final class Randomness {
private static Random getWithoutSeed() {
assert currentMethod == null && getRandomMethod == null : "running under tests but tried to create non-reproducible random";
return RandomnessHelper.LOCAL.get();
return ThreadLocalRandom.current();
}
public static void shuffle(List<?> list) {
Collections.shuffle(list, get());
}
private static class RandomnessHelper {
private static final SecureRandom SR = new SecureRandom();
private static final ThreadLocal<Random> LOCAL = ThreadLocal.withInitial(
() -> {
byte[] bytes = SR.generateSeed(8);
long accumulator = 0;
for (int i = 0; i < bytes.length; i++) {
accumulator = (accumulator << 8) + bytes[i] & 0xFFL;
}
return new Random(accumulator);
}
);
}
}