src/share/classes/java/util/UUID.java

Print this page

        

@@ -88,13 +88,15 @@
      */
     private final long leastSigBits;
 
     /*
      * The random number generator used by this class to create random
-     * based UUIDs.
+     * based UUIDs. In a holder class to defer initialization until needed.
      */
-    private static volatile SecureRandom numberGenerator = null;
+    private static class Holder {
+        static final SecureRandom numberGenerator = new SecureRandom();
+    }
 
     // Constructors and Factories
 
     /*
      * Private constructor which uses a byte array to construct the new UUID.

@@ -135,14 +137,11 @@
      * random number generator.
      *
      * @return  A randomly generated {@code UUID}
      */
     public static UUID randomUUID() {
-        SecureRandom ng = numberGenerator;
-        if (ng == null) {
-            numberGenerator = ng = new SecureRandom();
-        }
+        SecureRandom ng = Holder.numberGenerator;
 
         byte[] randomBytes = new byte[16];
         ng.nextBytes(randomBytes);
         randomBytes[6]  &= 0x0f;  /* clear version        */
         randomBytes[6]  |= 0x40;  /* set to version 4     */

@@ -253,21 +252,21 @@
      * number describes the layout of the {@code UUID}.
      *
      * The variant number has the following meaning:
      * <p><ul>
      * <li>0    Reserved for NCS backward compatibility
-     * <li>2    The Leach-Salz variant (used by this class)
+     * <li>2    IETF RFC 4122 (Leach-Salz) (used by this class)
      * <li>6    Reserved, Microsoft Corporation backward compatibility
      * <li>7    Reserved for future definition
      * </ul>
      *
      * @return  The variant number of this {@code UUID}
      */
     public int variant() {
         // This field is composed of a varying number of bits.
         // 0    -    -    Reserved for NCS backward compatibility
-        // 1    0    -    The Leach-Salz variant (used by this class)
+        // 1    0    -    The Leach-Salz aka IETF variant (used by this class)
         // 1    1    0    Reserved, Microsoft backward compatibility
         // 1    1    1    Reserved for future definition.
         return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
                       & (leastSigBits >> 63));
     }