While doing something completely unrelated, I ended up inadvertently browsing java.util.Random src, and noticed…
private static Random randomNumberGenerator;
private static synchronized Random initRNG() {
Random rnd = randomNumberGenerator;
return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
}
public static double random() {
Random rnd = randomNumberGenerator;
if (rnd == null) rnd = initRNG();
return rnd.nextDouble();
}
Isn’t that an obtuse implementation of double-checked locking?
I thought out of order writes made the double-checked locking idiom all kinds of bad?
No doubt if it is a bug it’d be hideously complicated to detect it in any kind of unit test.