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 to 10 different IDs per second
- Has to be unique (even if the application re-starts)
- Not possible to save a number in the Database
- As fast as possible NOT to add much lattency to the system
The best solution I found so far is the following:
private static int inc = 0;
private static long getId(){
long id = Long.parseLong(String.valueOf(System.currentTimeMillis())
.substring(1,10)
.concat(String.valueOf(inc)));
inc = (inc+1)%10;
return id;
}
This solution has the following problems:
- If for any reason there is a need to create more than 10 IDs per seccond, this solution won't work.
- In about 32 years this ID could be repeated (This is probably acceptable)
Any other solution to create this ID?
Any other problem I haven't thought of with mine?
Thanks for your help,