I am using the UUID.randomUUID().toString() to append a unique value to a string that is ultimately stored in the database and has unique constraint on it
But because my application is multi-threaded the execution happens at the same time of UUID generation and ultimately the same UUID is appended to the string and persistence fails.
Is there a better way to generate random string i.e. a fail-safe method.
I tried debugging and when I paused the other threads and let them go one by one then it worked fine.
I am currently using the following code to make it more random but i do not like this approach.
Random r = new Random();
List<Integer> uniqueNUmbers = new ArrayList<>();
for (int i=0;i<10;i++) {
int x=r.nextInt(9999);
while (uniqueNumbers.contains(x))
x=r.nextInt(9999);
uniqueNumbers.add(x);
}
String string = String.format("%04d", uniqueNumbers.get(0));
string = uuid + string;
But this is like a hacky code. And I dont like this.
Does any one know a fail-proof method to actually geenrate a random string.
UUIDs are handy for giving entities their own special names, for example, in a database. There are several ways to generate them, including methods based on time, MAC addresses, hashes, and random numbers, but they make the same promise: no two are identical. Each one is unique across space and time.
No, a UUID can't be guaranteed to be unique. A UUID is just a 128-bit random number. When my computer generates a UUID, there's no practical way it can prevent your computer or any other device in the universe from generating that same UUID at some time in the future.
This number is equivalent to generating 1 billion UUIDs per second for about 85 years. A file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes. Thus, the probability to find a duplicate within 103 trillion version-4 UUIDs is one in a billion.
a uuid is “in theory” a 128-bit random number. that's a HUGE number (like atoms in the universe big) and if the algorithms were truly random (which they're not) it would be effectively impossible to have a duplicate.
You can synchronize uuid-generation method, and/or you can pregen uuid pool and generate more identificators in one thread when pool starts to deplete.
I honestly think you are barking up the wrong tree. The real problem here is the (apparently) non-thread-safe way that you are using UUID.randomUUID
. (You didn't show us the code for that, so we can't spot the actual issue for you!) The best solution would be to find and fix the thread safety problem.
Don't try implementing your own random/unique string generator. Doing that will just create more problems down the track.
For example, your use of Random
like this would effectively limit you to ~2^48 distinct UUIDs ... because that is the seed length for Random
; see https://docs.oracle.com/javase/8/docs/api/java/util/Random.html. ThreadLocalRandom
has the same problem.
Even if you get your implementation correct, you still need to deal with the underlying thread safety issue. And you have added a bunch of (IMO) unnecessary code to your code base that needs thorough unit testing ... and has to be maintained for the lifetime of your code base.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With