0

Hi i know the workings of ConcurrentHashMap before JDK 8. I also understood the code: it was pretty modular and not very hard to understand.

The code of ConcurrentHashMap in JDK 8 has changed a lot from its previous implementations.

Because this question was classified as too broad I will now try to be very specific.

CHMv8 uses a TreeBin (a variant of RedBlackTree) for buckets rather than a linked list.

So my question what is the main advantage of using TreeBin over a linked list?

Source code here

user207421
  • 305,947
  • 44
  • 307
  • 483
veritas
  • 2,444
  • 1
  • 21
  • 30
  • 1
    First, there's only ~3k lines of code. – Dave Newton Jul 21 '14 at 19:12
  • Besides which, since when is 6K lines of code "enormous"? – Hot Licks Jul 21 '14 at 19:14
  • @HotLicks 6KLOC is a pretty monstrous single class. CHM gets some slack for including a bunch of nested classes. – chrylis -cautiouslyoptimistic- Jul 21 '14 at 19:15
  • @DaveNewton I think the OP was also counting comments + javadoc in the 6k total – dkatzel Jul 21 '14 at 19:18
  • @dkatzel Obviously, but they don't add to the complexity of the code. It's not 6KLoC. – Dave Newton Jul 21 '14 at 19:30
  • @DaveNewton I think i'll edit my question for better language. – veritas Jul 21 '14 at 20:04
  • @DaveNewton hi Dave i have edited my question focusing on the main issue i wanted to ask. Thanks for pointing out why my question was unconstructive – veritas Jul 21 '14 at 20:09
  • @Todd if you think if the edited question is now constructive and not too broad please vote to reopen it. Or suggest me to make the question better or anyother place where i can get my answer. thanks all – veritas Jul 21 '14 at 20:34
  • It's still "too broad" as the term is used here. Stack Overflow is [a Q&A resource, not a help forum](http://meta.stackoverflow.com/a/92115/228805). It's for specific questions about coding that can have definite correct answers. A question pointing to some code and asking in general terms for help understanding what it does or how it works is off-topic, as are requests for references, tutorials, or feature comparisons between products. You need to identify a specific question about the code--what part don't you understand, and what specifically don't you understand about it? – Adi Inbar Jul 21 '14 at 20:48
  • Also, ***include all relevant information in the question*** rather than linking. Please read the following advice on asking questions: [Writing the perfect question](http://goo.gl/1tBMnR). – Adi Inbar Jul 21 '14 at 20:48
  • @AdiInbar thanks. I have narrowed down the question to be very specific. If you think if its not to broad. Please vote to reopen – veritas Jul 21 '14 at 21:07
  • @Eran Hi i edited my question to very specific, if you think its fine please vote for reopen – veritas Jul 21 '14 at 21:07
  • @DonBranson Hi i have edited my question to very specific, if you think its fine please vote for reopen – veritas Jul 21 '14 at 21:08
  • See http://openjdk.java.net/jeps/180 “*In the case of high hash collisions, this will improve worst-case performance from O(n) to O(log n).*” – Holger Aug 22 '14 at 10:46
  • @Holger thanks for the link its valuable – veritas Aug 23 '14 at 17:31

1 Answers1

4

The main changes are to add ConcurrentHashMap specific implementations of the new Java 8 default Map methods with better concurrent implementations that rely on the internal details. These changes required lots of new inner classes which bloat the .java file

For example, some of these methods include:

compute(K key, BiFunction remappingFunction)

forEach(BiConsumer action)

merge(K key, V value, BiFunction remappingFunction)

Just to name a few.

I think this also shows why you usually shouldn't care about implementation details on how a class you don't have to maintain works. As long as the class follows the contract laid out in its javadoc, you should be agnostic of how it works since the implementation details can change in the future.

dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • thanks i was looking for this kind of response only. Implementation is important for me as am writing some niche data structures to handle concurrency but for very specific requirements. I tried to take good things various HasHMap implementations and various trade off. i dont' need all the functionality of CHMv8 just some.. for example computeIfAbsent. Also wanted to know how they take care of resizing? – veritas Jul 21 '14 at 20:19
  • I think this also shows why you usually shouldn't care about implementation details on how a class you don't have to maintain works Until you run into problems, of course. – michaelok Apr 26 '16 at 16:59