Java: Unique 10 digit ID

后端 未结 4 1774
渐次进展
渐次进展 2020-12-13 15:57

I need to generate a unique 10 digit ID in Java. These are the restrictions for this ID:

  • Only Numeric
  • Maximum 10 digits
  • Possible to create up
4条回答
  •  甜味超标
    2020-12-13 16:35

    What means that it has to be unique? Even across more currently running instances? It break your implementation.

    If it must be unique across universe, the best solution is to use UUID as it's mathematically proven identifier generator as it generates unique value per universe. Less accurate number brings you collisions.

    When there is only one concurrent instance, you can take current time in millis and solve 10ms problem using incrementation. If you sacrifice proper number of last positions in the number you can get many number within one milliseconds. I would than define the precision - I mean how much unique numbers do you need per seconds. You will solve the issue without any persistence using this approach.

提交回复
热议问题