• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Concurrent Hashmap Internals

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just trying to understand how concurrent hashmap works internally? When we say it has 16 segments, what does it mean - how it can have 16 threads writing in parallel to the same map?
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who said anything about 16 segments? What is a segment? Have you tried going through the source code?
 
Saloon Keeper
Posts: 14507
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In previous topics you've made about ConcurrentHashMap you've mentioned 'segment' without ever answering us what you mean by that. It's your turn.
 
Rancher
Posts: 4334
59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java 7 implementation of ConcurrentHashMap used a class called Segment, internally.  Since Java 8 Segment has been gone, and replaced with bucket-level locking.  So if you really want to understand the Java 7 version, cool, look at the source code for that - but don't expect that to tell you how it works in Java 8 or later.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan, Campbell, I read on some blogs and websites about the segments. Not on oracle website though. I apologize for not replying earlier.

https://dzone.com/articles/how-concurrenthashmap-works-internally-in-java
https://javahungry.blogspot.com/2015/02/how-concurrenthashmap-works-in-java-internal-implementation.html

So, do we have one hashtable or 16 hashtables in concurrenthashmap considering defaults for concurrency control.
 
Campbell Ritchie
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would appear the first link is out of date.
The second says the Map locks its individual buckets, so that wouldn't mean it contains multiple hash tables. The second also says this is a popular interview question. Which should be countered with, “Do I actually want to work there?”
 
Saloon Keeper
Posts: 26295
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It would appear the first link is out of date.
The second says the Map locks its individual buckets, so that wouldn't mean it contains multiple hash tables. The second also says this is a popular interview question. Which should be countered with, “Do I actually want to work there?”



I don't know. If you were a tree, what kind of tree would you be?  
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html
 
Campbell Ritchie
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Whose blog is that?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic