Created
July 30, 2019 01:27
-
-
Save ggdio/46224189921abf3a97a8a8908ad69296 to your computer and use it in GitHub Desktop.
A concurrent random UUID generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConcurrentUUIDGenerator { | |
public static UUID randomUUID() { | |
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 &= 0xffffffffffff0fffL; | |
mostSig |= 0x0000000000004000L; | |
// 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 &= 0x3fffffffffffffffL; | |
leastSig |= 0x8000000000000000L; | |
return new UUID(mostSig, leastSig); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment