This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Concurrent Hashmap Internals

 
Ranch Hand
Posts: 431
2
Java
  • Mark post as helpful
  • send pies
  • 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: 70727
288
  • Mark post as helpful
  • send pies
  • 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: 12442
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Master Rancher
Posts: 3704
44
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 431
2
Java
  • Mark post as helpful
  • send pies
  • 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: 70727
288
  • Mark post as helpful
  • send pies
  • 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: 22679
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html
 
Campbell Ritchie
Marshal
Posts: 70727
288
  • Mark post as helpful
  • send pies
  • 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