--- old/src/share/classes/java/util/UUID.java 2012-05-10 15:56:40.000000000 -0700 +++ new/src/share/classes/java/util/UUID.java 2012-05-10 15:56:40.000000000 -0700 @@ -90,9 +90,11 @@ /* * 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 @@ -137,10 +139,7 @@ * @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); @@ -255,7 +254,7 @@ * The variant number has the following meaning: *

@@ -265,7 +264,7 @@ 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))) --- old/test/java/util/UUID/UUIDTest.java 2012-05-10 15:56:42.000000000 -0700 +++ new/test/java/util/UUID/UUIDTest.java 2012-05-10 15:56:42.000000000 -0700 @@ -58,6 +58,12 @@ List list = new LinkedList(); for (int i=0; i<100; i++) { UUID u1 = UUID.randomUUID(); + if(4 != u1.version()) { + throw new Exception("bad version"); + } + if(2 != u1.variant()) { + throw new Exception("bad variant"); + } if (list.contains(u1)) throw new Exception("random UUID collision very unlikely"); list.add(u1); @@ -70,10 +76,16 @@ List list = new LinkedList(); for (int i=0; i<100; i++) { byteSource.nextBytes(someBytes); - UUID test = UUID.nameUUIDFromBytes(someBytes); - if (list.contains(test)) + UUID u1 = UUID.nameUUIDFromBytes(someBytes); + if(3 != u1.version()) { + throw new Exception("bad version"); + } + if(2 != u1.variant()) { + throw new Exception("bad variant"); + } + if (list.contains(u1)) throw new Exception("byte UUID collision very unlikely"); - list.add(test); + list.add(u1); } }