为什么我的 UUID 使用了太多时间?
String s = UUID.randomUUID().toString();
return s.substring(0,8) + s.substring(9,13) + s.substring(14,18) +
s.substring(19,23) + s.substring(24);
我使用JDK1.5的UUID,但是当我连接/断开网络时它花费了太多时间。
我认为 UUID 可能想要访问某些网络。
有谁能够帮助我?
String s = UUID.randomUUID().toString();
return s.substring(0,8) + s.substring(9,13) + s.substring(14,18) +
s.substring(19,23) + s.substring(24);
I use JDK1.5's UUID, but it uses too much time when I connect/disconnect from the net.
I think the UUID may want to access some net.
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
UUID 生成在本地完成,不需要任何活动的网络连接。
UUID generation is done locally and doesn't require any alive network connection.
引用 API odc:
您的延迟可能是由加密强 RNG 的初始化引起的 - 这需要一些时间,甚至可能取决于作为熵源的网络连接的存在。 然而,这应该在 JVM 运行时只发生一次。 不过,我没有找到解决这个问题的方法。
Quoting the API odc:
Your delay is probably being caused by the intialization of the cryptographically strong RNG - those take some time, and might even depend on the presence of a network connection as a source of entropy. However, this should happen only once during the runtime of the JVM. I don't see a way around this problem, though.
UUID 的 javadoc http://java. sun.com/j2se/1.5.0/docs/api/java/util/UUID.html 有一些关于如何生成 UUID 的好信息。 它使用时间和时钟频率来生成 UUID。 正如 Sharptooth 所说,不需要网络接口。 是否可能有其他正在运行的并发进程可能导致此问题?
The javadoc for UUID http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html has some good information on how the UUID is generated. It uses the time and clock frequency to generate the UUID. Like sharptooth says, no network interface is required. Is there possibly some other concurrent process running that could possibly be causing this problem?
这些 s.substring 调用的目的是什么? 看起来您正在返回原始字符串。
What's the purpose of those s.substring calls? It looks like you're returning the original string.
如果您将 5 个字符串附加到一大组数据上,这可能就是问题所在。 尝试使用StringBuffer。 将超过 1-2 个字符串连接在一起时会产生令人惊讶的差异,尤其是对于较大的数据集
If you're appending 5 Strings together, over a large set of data, that could be the issue. Try to use StringBuffer. It's amazing the difference that can make when concatenating more than 1-2 Strings together, especially for larger datasets
对于旧版本的 Java(也许是 6 及更早版本?),Random 中存在一个错误,导致它迭代整个临时目录。 我们发现,在 NVIDIA 的一些极其糟糕的构建机器上,种子生成需要 10 分钟。 您可能想检查临时目录的大小。
比较: http://www.docjar.com/html /api/sun/security/provider/SeedGenerator.java.html
至: http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules/j2me/sun/security/provider/SeedGenerator.java.htm
For older versions of Java (6 and earlier maybe?), there's a bug in Random that causes it to iterate over the entire temp directory. We've seen seed generation take 10 minutes on some egregiously bad build machines at NVIDIA. You might want to check the size of your temp dir.
Compare: http://www.docjar.com/html/api/sun/security/provider/SeedGenerator.java.html
To: http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules/j2me/sun/security/provider/SeedGenerator.java.htm