我需要在 Java 中创建一些唯一文件,并且我计划使用 UUID.randomUUID 来生成它们的名称。有没有机会为此发生碰撞?我应该做类似波纹管操作系统的事情吗?我不应该担心这个?
Integer attemptsToGenerateUUID = 1;
while (true) {
UUID fileUUID = UUID.randomUUID();
if (fileDoesNotExistwith this UUID name) {
save file;
break;
}
attemptsToGenerateUUID += 1;
if (attemptsToGenerateUUID > 64) {
return false;
}
}
最佳答案
根据wikipedia ,关于随机 UUID 中重复的概率:
Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs.
我想同样的推理也适用于 Java 的 UUID 实现。所以不,你不应该担心这个。
关于java - 发生 Java UUID.randomUUID 冲突的机会有多大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876188/