在 Java 中出于调试目的打印唯一的对象标识

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14499969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:28:30  来源:igfitidea点击:

Printing unique object identity for debugging purposes in Java

javaobjectidentityuniqueidentifier

提问by hyde

Let's start with code.

让我们从代码开始。

Future<MyResult> obj = getFuture();
debugLog.println("Handling future: " + System.identityHashCode(obj);

and then somewhere else same again, or possibly the same piece of code executed again, possibly in different thread.

然后在其他地方再次相同,或者可能在不同的线程中再次执行同一段代码。

Future<MyResult> obj = getFuture();
debugLog.println("Handling future: " + System.identityHashCode(obj);

Now above, if obj's are same object, the debug prints will be same, obviously. But if they are different, there's still a chance of hash collision, so the printout maybe same even for different objects. So having same output (or more generally, same string) does not guarantee same object.

现在上面,如果 obj 是同一个对象,调试打印将是相同的,显然。但是如果它们不同,仍然有可能发生哈希冲突,因此即使对于不同的对象,打印输出也可能相同。因此,具有相同的输出(或更一般地说,相同的字符串)并不能保证相同的对象。

Question: Is there a way in Java to get unique id stringfor arbitrary object? Or more formally, give static String Id.str(Object o)method so that so that this is alwaystrue:

问题:Java 中有没有办法为任意对象获取唯一的 id字符串?或者更正式地,给出static String Id.str(Object o)方法,以便这总是正确的:

final Object obj1 = ...;
final String firstId = Id.str(obj1);
// arbitrary time passes, garbage collections happen etc.
// firstId and obj1 are same variables as above
(firstId.equals(Id.str.obj2)) == (obj1 == obj2)

回答by Andremoniy

I think, that actually there isn't any method (i.e. technique) which will guarantee you such uniqueness of object's ID. The only purpose of such identification - is a method for addressing to this object's data in memory. In case when this object dies and then being removed from memory by GC, nobody will restrict system to use its former address space for placing new object's data.

我认为,实际上没有任何方法(即技术)可以保证您对象 ID 的这种唯一性。这种识别的唯一目的 - 是一种用于寻址该对象在内存中的数据的方法。万一这个对象死了然后被GC从内存中删除,没有人会限制系统使用它以前的地址空间来放置新对象的数据。

But in fact, during some short period, among all available objects, which have any references inside your program, System.identityHashCode(obj)will actually give you uniqueidentity of object. This is because this hashCodeis calculated using object's in-memory location. However, it is an implementation feature, which is not documented and guaranteed for foreign JVMs.

但实际上,在很短的时间内,在所有可用的对象中,在你的程序中有任何引用的,System.identityHashCode(obj)实际上会给你唯一的对象标识。这是因为这hashCode是使用对象的内存位置计算的。但是,它是一个实现特性,对于外部 JVM 没有记录和保证。

Also, you can read this famous QA: Java: How to get the unique ID of an object which overrides hashCode()?

此外,您可以阅读这个著名的 QA:Java:如何获取覆盖 hashCode() 的对象的唯一 ID?

回答by codeghost

Assuming it is not important for this ID to actually be the hashcode then you could guarantee a unique ID by having a Singleton class generating ID's that are assigned to each object. You can then use that identifier rather than the hashcode.

假设此 ID 实际上是哈希码并不重要,那么您可以通过让单例类生成分配给每个对象的 ID 来保证唯一的 ID。然后您可以使用该标识符而不是哈希码。

What is your objective, perhaps there are better solitions?

您的目标是什么,也许有更好的解决方案?