当前位置: 代码迷 >> Android >> Will android (java) UUID.randomUUID() return repeated UUIDs
  详细解决方案

Will android (java) UUID.randomUUID() return repeated UUIDs

热度:135   发布时间:2016-04-28 07:17:41.0
Will android (java) UUID.randomUUID() return repeated UUIDs?
Theoretically, I don't think this method will return repeated UUIDs, even from different devices.

Let's check the documentation of android: http://developer.android.com/reference/java/util/UUID.html. It says "Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122."

Even in JDK documentation http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html , it says they are implementing RFC 4122.

And in RFC 4122, http://www.ietf.org/rfc/rfc4122.txt, it says "A UUID is 128 bits long, and can guarantee uniqueness across space and time."

Wikipedia also calculates the probability of duplicates of RandomUUID: http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates

There is a guy here (http://www.coderanch.com/t/385404/java/java/UUID-Uniqueness-JVMs) states that "We (i.e. my team at work) have been using the Java Util UUID class for UUIDs across JVMs."

However, I still can see a lot of worries that the Java randomUUID will not be unique.

First of all, let us check the wikipedia:http://en.wikipedia.org/wiki/Universally_unique_identifier. It says java is not using MAC address for generating UUID "due to lack of means to access MAC addresses using pure Java before version 6". It also gives opensource UUID generation tools as http://johannburkard.de/software/uuid/ and https://github.com/cowtowncoder/java-uuid-generator. Not sure not using MAC address will make them not unique.

Some people here (http://www.coderanch.com/t/385404/java/java/UUID-Uniqueness-JVMs) also say they are using a central control to generate UUID.

This link(http://stackoverflow.com/questions/2513573/how-good-is-javas-uuid-randomuuid ) says "implementation to contain subtle bugs that ruin all this (see OpenSSH key generation bug)".

This link(http://stackoverflow.com/questions/6505837/is-oracles-sys-guid-uuid-rfc-4122-compliant) says "SYS_GUID is Oracle's equivalent of UUID. It is globally unique. However, it is not compliant to RFC 4122".

Anyway, I do not find any evidence that says UUID.randomUUID() will repeat, even from different machines.
As it says here (http://stackoverflow.com/questions/2513573/how-good-is-javas-uuid-randomuuid), "I don't think there's any concrete reason to worry about Java UUIDs's randomness."

However, if we are building very critical distributed applications with a lot of data, maybe, we can consider using MAC address algorithm, or using central UUID generator.
?
  相关解决方案