Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no ConcurrentLinkedHashMap class in jdk?

This question follows directly from my previous question here in SO . I think the answer to my second question is no . So I would like understand why there is no ConcurrentLinkedHashMap in java.util.concurrent package ? I mean there is a ConcurrentHashMap but no ConcurrentLinkedHashMap . Does it not make any sense at all to have such a class in Concurrent environments ? I mean what is the main technical reason here for its non availabalility ? Is there something similar in Guava/ Apache Commons ?

like image 589
Geek Avatar asked Sep 06 '12 12:09

Geek


3 Answers

Why there is no ConcurrentLinkedHashMap class in jdk?

You would need to ask the Oracle Java guys that, but I imagine that it is a combination of:

  • a perception that not many people would need it, and
  • the inherent difficulties in implementing data structures with good performance properties in highly concurrent use cases.

In this case, it seems to me that implementing the collection class so that iterating the key/value/entry sets is not a concurrency bottleneck would be ... um ... difficult. (And even if people have figured a way to do it, the fact remains that designing and implementing and proving the correctness of general purpose highly concurrent data structures and algorithms is hard.)

like image 187
Stephen C Avatar answered Oct 27 '22 09:10

Stephen C


Looks like there is one from Google https://code.google.com/p/concurrentlinkedhashmap/

Check also this post: What does it mean that ConcurrentLinkedHashMap has been integrated into Guava?

like image 45
Arek Avatar answered Oct 27 '22 09:10

Arek


#define PERSONAL_OPINION

From a design point of view, it makes more sense to always have to use

Map m = Collections.synchronizedMap(new HashMap());
  ...
Set s = m.keySet();  // Needn't be in synchronized block
  ...
synchronized(m) {  // Synchronizing on m, not s!
   Iterator i = s.iterator(); // Must be in synchronized block
   while (i.hasNext())
      foo(i.next());
}

example in synchronizedMap

Why? Because the synchronization mechanism is tied to a high abstraction (the Map interface). But assuming I am right there can be two reasons to still have ConcurrentHashMap:

  • Either ConcurrentHashMap exists before this sync mechanism
  • Either there is a performance gain in creating a specific synchronized mechanism.

My point is in the ideal world of design even ConcurrentHashMap should not exist.

#end //personal opinion
like image 45
UmNyobe Avatar answered Oct 27 '22 10:10

UmNyobe