private static UUID generateRandomUuid ( ) { final Random rnd = ThreadLocalRandom . current ( ) ; long mostSig = rnd . nextLong ( ) ; long leastSig = rnd . nextLong ( ) ; // Identify this as a version 4 UUID, that is one based on a random value. mostSig &= 0xffffffffffff0fff L ; mostSig |= 0x0000000000004000 L ; // Set the variant identifier as specified for version 4 UUID values. The two // high order bits of the lower word are required to be one and zero, respectively. leastSig &= 0x3fffffffffffffff L ; leastSig |= 0x8000000000000000 L ; return new UUID ( mostSig , leastSig ) ; }
Generate a random UUID v4 that will perform reasonably when used by multiple threads under load .