是否有可能为特定的JVM实例获取真正唯一的ID? [英] Is it possible to get a truly unique id for a particular JVM instance?

查看:387
本文介绍了是否有可能为特定的JVM实例获取真正唯一的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种从JVM中运行的Java代码中唯一且永久地识别JVM实例的方法。

I need a way to uniquely and permanently identify an instance of the JVM from within Java code running in that JVM.

也就是说,如果我在同一台机器上同时运行两个JVM,则每个JVM都是可区分的。
即使重用进程ID,它也可以在其他机器上运行JVM,也可以在同一台机器上运行。

That is, if I have two JVMs running at the same time on the same machine, each is distinguishable. It is also distinguishable from running JVMs on other machines and from future executions on the same machine even if the process id is reused.

我想我可以通过识别开始时间,机器MAC和进程ID来实现这样的东西,并以某种方式组合它们。我想知道是否有一些标准的方法来实现这一点。

I figure I could implement something like this by identifying the start time, the machine MAC, and the process id, and combining them in some way. I'm wondering if there is some standard way to achieve this.

更新:我看到每个人都推荐了整个会话的UUID。这似乎是一个好主意,虽然可能有点太重量级了。这是我的问题:我想使用JVM id在每个JVM执行中创建多个唯一标识符,以某种方式合并JVM实例。

Update: I see that everyone recommended a UUID for the entire session. That seems like a good idea though possibly a little too heavyweight. Here is my problem though: I want to use the JVM id to create multiple unique identifiers in each JVM execution that somehow incorporate the JVM instance.

我的理解是你不应该将其他数字混合到一个UUID中,因为不再保证唯一性。另一种方法是将UUID转换为字符串并将其链接,但随后它变得太长。克服这个想法?

My understanding is that you shouldn't really mix other numbers into a UUID because uniqueness is no longer guaranteed. An alternative is to make the UUID into a string and chain it, but then it becomes too long. Any ideas on overcoming this?

推荐答案

您可以在程序开头生成 UUID 在程序运行期间使用它。

You could generate a UUID at the start of your program and use that during the time the program is running.

UUID id = UUID.randomUUID();

除方法 randomUUID()外,还有生成 UUID 的其他方法可能更符合您的需求,请参阅类 java.util.UUID 的API文档了解更多信息。

Besides the method randomUUID() there are other methods to generate UUIDs that might fit your needs better, see the API documentation of class java.util.UUID for more information.

这篇关于是否有可能为特定的JVM实例获取真正唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆