How to produce a True UUID in Java?

I was under the impression that the UUID spec required a guaranteed, true, globally unique result, not unique 99.99999999999% of the time, but truly 100% of the time. From the spec:

A UUID is 128 bits long, and can guarantee uniqueness across space and time.

It looks like java only support V3 and V4 of the UUID spec. V4 isn't truly unique. With the V3 implementation using nameUUIDFromBytes , the following results in duplicates, because the computer is too fast (edit: looping to 10 and called new Date().getTime() will produce duplicates because the computer loops faster than new Date().getTime() can produce a different value on each iteration):

String seed;
for (int i = 0; i < 10; i++) {
     seed = "<hostname>" + new Date().getTime();
    System.out.println(java.util.UUID.nameUUIDFromBytes(seed.getBytes()));
}

Am I mistaken in assuming that a UUID is 100% unique, and that it is only practically unique but not perfectly so? Is there anyway to do this in Java?


There are different methods of UUID generation. The kind you're using is behaving exactly as it should. You're using nameUUIDFromBytes , a "Static factory to retrieve a type 3 (name based) UUID based on the specified byte array."

This generates the same UUID if given the same name. As you've discovered, your loop is passing-in the same name every time, so you get the same UUID.

Have a look at Gabe's advice here: Which UUID version to use? He recommends you use V4, which as others have pointed out is good enough for any realistic use case.


Because your entropy is limited to your memory, you can never ensure a UUID is "guaranteed, true, globally unique result". However, 99.99999999999% is already pretty good.

If you want to ensure unique values in your database, you could use a simple integer that's incremented to be sure it's unique. If you want to use UUIDs and be really sure they're unique, you just have to check that upon creation. If there's a duplicate, just create another one until it's unique.

Duplicates can happen, but IIRC, part of them is created dependent on your current time, so if you're just creating one every 5 minutes, you should be safe.


As others have pointed out, the type-4 UUID returned by UUID.randomUUID() is likely to be unique enough for any practical application. Cases where it's not are likely to be pathological: for example, rolling back a VM to a live snapshot, without restarting the Java process, so that the random-number generator goes back to an exact prior state.

By contrast, a type-3 or type-5 UUID is only as unique as what you put into it.

A type-1 UUID (time-based) should be very slightly "more" unique, under certain constraints. The Java platform does not include support for generating a type-1 UUID, but I've written code (possibly not published) to call a UUID generating library via JNI. It was 18 lines of C and 11 lines of Java.

链接地址: http://www.djcxy.com/p/91490.html

上一篇: 为什么.NET GUID中有破折号?

下一篇: 如何在Java中生成真正的UUID?