在Java中生成全局唯一标识符 [英] Generating a globally unique identifier in Java

查看:947
本文介绍了在Java中生成全局唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:我正在开发一个持久的Java Web应用程序,我需要确保我持有的所有资源都有全局唯一标识符以防止重复。

Summary: I'm developing a persistent Java web application, and I need to make sure that all resources I persist have globally unique identifiers to prevent duplicates.

精美打印:


  1. 我没有使用RDBMS,所以我不喜欢没有任何花哨的序列生成器(例如Oracle提供的那个)

  2. 我希望它快速,最好全部在内存中 - 我宁愿不用打开一个文件并增加一些值

  3. 它需要是线程安全的(我预计一次只有一个JVM需要生成ID)

  4. JVM的实例化需要保持一致。如果服务器关闭并启动,ID生成器不应该重新生成它在先前实例化中生成的相同ID(或者至少机会必须非常,非常小 - 我预计会有数百万个预先存储的资源)

  5. 我已经看到了EJB唯一ID模式文章中的示例。它们不适合我(我宁愿不仅仅依靠System.currentTimeMillis(),因为我们将每毫秒保持多个资源)。

  6. 我看了答案在这个问题中提出。我对它们的关注是,随着时间的推移,我将获得重复ID的可能性是多少?我对使用 java的建议很感兴趣 UUID 的.util.UUID ,但同样重复的可能性需要无限小。

  7. 我正在使用JDK6

  1. I'm not using an RDBMS, so I don't have any fancy sequence generators (such as the one provided by Oracle)
  2. I'd like it to be fast, preferably all in memory - I'd rather not have to open up a file and increment some value
  3. It needs to be thread safe (I'm anticipating that only one JVM at a time will need to generate IDs)
  4. There needs to be consistency across instantiations of the JVM. If the server shuts down and starts up, the ID generator shouldn't re-generate the same IDs it generated in previous instantiations (or at least the chance has to be really, really slim - I anticipate many millions of presisted resources)
  5. I have seen the examples in the EJB unique ID pattern article. They won't work for me (I'd rather not rely solely on System.currentTimeMillis() because we'll be persisting multiple resources per millisecond).
  6. I have looked at the answers proposed in this question. My concern about them is, what is the chance that I will get a duplicate ID over time? I'm intrigued by the suggestion to use java.util.UUID for a UUID, but again, the chances of a duplicate need to be infinitesimally small.
  7. I'm using JDK6


推荐答案

非常确定UUID足够好。有340,282,366,920,938,463,463,374,607,431,770,000,000 UUID可供使用。

Pretty sure UUIDs are "good enough". There are 340,282,366,920,938,463,463,374,607,431,770,000,000 UUIDs available.

http: //www.wilybeagle.com/guid_store/guid_explain.htm

为了对这些数字进行透视,每年被陨石击中的风险是估计是170亿的一次机会,这意味着概率约为0.00000000006(6×10-11),相当于一年内创造几十万亿UUID并且有一个重复的几率。换句话说,只有在未来100年内每秒生成10亿UUID后,创建一个重复的概率约为50%。如果地球上每个人拥有6亿UUID,则一次重复的概率约为50%

"To put these numbers into perspective, one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, that means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs"

http://en.wikipedia.org/wiki/Universally_Unique_Identifier

这篇关于在Java中生成全局唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆