Bug ID: JDK-4228660 Unnecessary extra locking in java.lang.Math.random

Here are some excerpts from the text of "Math.java":


    private static Random randomNumberGenerator;

    /* ...
     * This method is properly synchronized to allow correct use by more 
     * than one thread. However, if many threads need to generate 
     * pseudorandom numbers at a great rate, it may reduce contention for 
     * each thread to have its own pseudorandom-number generator.
     * ...
     */

    public static synchronized double random() {
        if (randomNumberGenerator == null)
            randomNumberGenerator = new Random();
        return randomNumberGenerator.nextDouble();
    }

This is a little silly.  All the "Random.next*" methods synchronize on
any modification to the random number generator's seed.  Thus, the
synchronization of this "random" method guards only the possible
initialization of the static field "randomNumberGenerator."

This is kind of expensive for something that only happens once.