This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Concurrenthashmap internal implementation in Java 8

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anybody please let me know how the concurrentHashMap implementation has been changed in Java 8.
As Far as I am aware the linked List nodes have been changes to Red Black Tree after a threshold of 8.
But I read somewhere that The backing array and locking has also been changed and now We are not using the Segment[] for locking.
Can someone please please provide some information regarding the same.
As to how the put() and get() works in CHM from java 8.

Thanks in advance.
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You doubtless know you can inspect the code by unzipping the src.zip file often included with Java® installations?
 
Aman Kashyap
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , I am aware of that
I tried checking that but couldn't understand the entire logic properly.
So just wanted to know if someone can give a jist of logic.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us some of the code and some from an older version.
 
Aman Kashyap
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK 1.7 Code :

JDK 1.8 Code:
 
As we can see, now the Segment class is striped down,
and Only present for backward compatibility.

I hope this source code helps.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bits about e.next in the Java7 version obviously constitute a linked list. Lines 11‑12 iterate the list looking to see whether the key is already included there. You will notice the necessity for a correct implementation of hashCode(). It is worth examining the code for the implementation of Map.Entry used here, which will have three fields, key, value, and next.
Note that unlike with ordinary HashMaps, the put(K, V) method doesn't appear to allow nulls.

Java8+ version. The mention of Nodes in putIfAbsent() does suggest that you have a binary tree implementation there. But I am afraid I am finding it difficult to follow and don't hae the time to go through it in detail.
 
Forget Steve. Look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!