Skip to content

Instantly share code, notes, and snippets.

@ggdio
Created July 30, 2019 01:27

Revisions

  1. ggdio created this gist Jul 30, 2019.
    20 changes: 20 additions & 0 deletions ConcurrentUUIDGenerator.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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);
    }

    }