generate UUID of long type

怎甘沉沦 提交于 2019-11-29 20:05:43

问题


Please give me sample code to generate UUID of long type in java without using timestamp.

Thanks


回答1:


A real UUID is 128 bits. A long is 64 bits.

This is not just pedantry. UUID stands for Universal Unique IDentifier.

The "universal uniqueness" of the established UUID schemes are based on:

  • encoding a MAC address and a timestamp,
  • encoding a hash of a DNS name and a timestamp, or
  • using a 122 bit random number ... which is large enough that the probability of a collision is very very small.

With 64 bits, there are simply not enough bits for "universal uniqueness". For instance, the birthday paradox means that if we had a number of computers generating random 64 bit numbers, the probability of a potentially detectable collision would be large enough to be of concern.

Now if you just want a UID (not a UUID), then any 64-bit sequence generator will do the job, provided that you take steps to guard against the sequence repeating. (If the sequence repeats, then the IDs are not unique in time; i.e. over time a given ID may denote different entities.)




回答2:


Have you looked at java.util.UUID?




回答3:


If you just want a simple unique long you can use AtomicLong.incrementAndGet(). This doesn't use a timestamp but does reset to 0 every time you start it and is not unique across JVMs.

What is the requirement not to use timestamps all about? UUID uses a timestamp. (amoungst other things)



来源:https://stackoverflow.com/questions/4529727/generate-uuid-of-long-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!