r/programming • • Apr 13 '20

Java Objects Inside Out: Everything You Ever Wanted to Know About Java Object Internals in Hotspot

https://shipilev.net/jvm/objects-inside-out/
37 Upvotes

3 comments sorted by

View all comments

1

u/Molossus-Spondee Apr 13 '20

I'm confused about the hashcode disables biased locking thing.

Does this mean if I make a utility object

final class Lock { }

as I tend to do instead of using an empty object for lock objects should I override hashcode?

5

u/haimez Apr 13 '20 edited Apr 14 '20

If those objects will ever have their hashCode method called, then yes. If not, there will be no difference.

Also, probably just use ReentrantLock anyway. For the medium term- project loom understands this better and just in general it’s more clear when your lock fields are real locks and not empty objects with implicit references to their enclosing object that you synchronize on.

3

u/shipilev Apr 14 '20

If those objects will ever have their hashCode method called, then yes. If not, there will be no difference.

Yes. I added the example in that section to make it a bit more clear.