1

While debugging I want to make sure that the Object in use (passed/modified in methods) by multiple threads is the same Object.

As in Java we can not find an object address, how to check if it is the same object? Is there a special way to find that in Netbeans or any other IDE?

SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
  • 2
    Are you looking for [System.identityHashCode()](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html)? – TheLostMind Oct 16 '14 at 09:32
  • Sounds like the job for `==`-Man! `ob1 == ob2` will return true if and only if the two references point to the same object. – biziclop Oct 16 '14 at 09:34
  • @TheLostMind No, other answers say that `IdentityHashCode` may not refer to the same object and can also have same value for different objects. – SMUsamaShah Oct 16 '14 at 09:34
  • @biziclop No, as the object is being modified inside methods `==` won't work. – SMUsamaShah Oct 16 '14 at 09:35
  • 1
    @LifeH2O How would it not work? Modification has got nothing to do with it. If it's the same reference, `==` will return true, if not, it won't. – biziclop Oct 16 '14 at 09:35
  • @LifeH2O - Can you point me to your reference(s)? – TheLostMind Oct 16 '14 at 09:36
  • @biziclop Will it work for HashMap? – SMUsamaShah Oct 16 '14 at 09:36
  • 1
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21.3 – biziclop Oct 16 '14 at 09:37
  • @biziclop is there a better way to use `==` in threads, because in a thread the object I want to compare might not be available. – SMUsamaShah Oct 16 '14 at 09:40
  • 2
    Note though, that if your access of the shared object isn't properly synchronized, it's entirely possible that two threads see different states, even though the object is the same. – biziclop Oct 16 '14 at 09:40
  • 1
    @TheLostMind http://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java http://stackoverflow.com/questions/580984/how-do-you-get-the-object-reference-of-an-object-in-java-when-tostring-and-h – SMUsamaShah Oct 16 '14 at 09:41
  • @LifeH2O - Ok. Thanks for that info :). Coming back to your question, why do you want to compare whether the object being passed is the same in 2 different threads?. And like *BiziClop* says, even though you pass the same object, different threads can see different results. – TheLostMind Oct 16 '14 at 09:44
  • @TheLostMind I am passing HashMap in threads and modifying it sometimes. While programming I had some issues doing that. It would have been a lot easier if in debug I could ensure that the HashMap is same in different threads. – SMUsamaShah Oct 16 '14 at 09:47
  • @TheLostMind In C++ one can easily find memory address to ensure. – SMUsamaShah Oct 16 '14 at 09:50
  • @LifeH2O - *HashMap* isn't thread-safe. You should use *ConcurrentHashMap* – TheLostMind Oct 16 '14 at 09:50
  • @TheLostMind How `==` will work while in another thread. e.g. How can I use `==` on the object of thread1 while inside thread2? identityHashCode looks like the only way in that case. – SMUsamaShah Oct 16 '14 at 09:59

1 Answers1

1

When using NetBeans then you can show at the Value in your variables window.

As long as the value is not a primitive datatype the number shows you some type of instance number for the object. As long as the number is the same the objcts are also the same.

E.g.

Map with instance number

If the size or something else is shown then you can configure your view by pressing the red marked configuration button.

Create a new Formatter as shown in the next picture (leave the Value code snippet blank) and deactivate the Default Formatter:

Map without size configuration

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
  • @LifeH2O changed answer to show how it can be done to get to work. – Uwe Plonus Oct 16 '14 at 11:41
  • That works. Can I add a column in there for that? Also a column which displays identityHashCode() for every object will be useful – SMUsamaShah Oct 16 '14 at 11:51
  • 1
    @LifeH2O You can enter _nearly_ whatever you want in the values field. So you can return the `identityHashCode()` together with the size or something else. But, as far as I know, you cannot add another column, which would b a nice feature... – Uwe Plonus Oct 16 '14 at 11:53