ArticleslgStudy

computer science

Java ConcurrentMap

Java ConcurrentMap is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java ConcurrentMap rather than just read about it. In short: The Java programming language's Java Collections Framework version 1.5 and later defines and implements the original regular single-threaded maps, and also new thread-safe maps implementing the java.util.concurrent.ConcurrentMap interface among other concurrent interfaces. In Java 1.6, the java.util.NavigableMap interface was added, extending java.util.SortedMap, and the java.util.concurrent.ConcurrentNavigableMap i…

Java ConcurrentMap — main illustration
Java ConcurrentMap — illustration

Key takeaways

  • Java ConcurrentMap belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java ConcurrentMap to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java ConcurrentMap from memory before moving on to harder problems.

Reference excerpt

The Java programming language's Java Collections Framework version 1.5 and later defines and implements the original regular single-threaded maps, and also new thread-safe maps implementing the java.util.concurrent.ConcurrentMap interface among other concurrent interfaces. In Java 1.6, the java.util.NavigableMap interface was added, extending java.util.SortedMap, and the java.util.concurrent.ConcurrentNavigableMap interface was added as a subinterface combination.

Java Map Interfaces The version 1.8 java.util.Map<K, V> interface diagram has the shape below. Sets can be considered sub-cases of corresponding maps in which the values are always a particular constant which can be ignored, although the java.util.Set<E> API uses corresponding but differently named methods. At the bottom is the java.util.concurrent.ConcurrentNavigableMap<K, V>, which is a multiple-inheritance.

java.util.Collection java.util.Map java.util.SortedMap java.util.NavigableMap java.util.concurrent.ConcurrentNavigableMap java.util.concurrent.ConcurrentMap java.util.concurrent.ConcurrentNavigableMap

Implementations

ConcurrentHashMap For unordered access as defined in the java.util.Map<K, V> interface, the java.util.concurrent.ConcurrentHashMap<K, V> implements java.util.concurrent.ConcurrentMap<K, V>. The mechanism is a hash access to a hash table with lists of entries, each entry holding a key, a value, the hash, and a next reference. Previous to Java 8, there were multiple locks each serializing access to a 'segment' of the table. In Java 8, native synchronization is used on the heads of the lists themselves, and the lists can mutate into small trees when they threaten to grow too large due to unfortunate hash collisions. Also, Java 8 uses the compare-and-set primitive optimistically to place the initial heads in the table, which is very fast. Performance is O ( n ) {\displaystyle O(n)} , but there are delays occasionally when rehashing is necessary. After the hash table expands, it never shrinks, possibly leading to a memory 'leak' after entries are removed.

ConcurrentSkipListMap For ordered access as defined by the java.util.NavigableMap<K, V> interface, java.util.concurrent.ConcurrentSkipListMap<K, V> was added in Java 1.6, and implements java.util.concurrent.ConcurrentMap<K, V> and also java.util.concurrent.ConcurrentNavigableMap<K, V>. It is a skip list which uses lock-free techniques to make a tree. Performance is O ( l o g ( n ) ) {\displaystyle O(log(n))} .

Concurrent modification problem One problem solved by the Java 1.5 java.util.concurrent<K, V> package is that of concurrent modification. The collection classes it provides may be reliably used by multiple java.lang.Threads. All thread-shared non-concurrent maps and other collections need to use some form of explicit locking such as native synchronization in order to prevent concurrent modification, or else there must be a way to prove from the program logic that concurrent modification cannot occur. Concurrent modification of a java.lang.Map<K, V> by multiple threads will sometimes destroy the internal consistency of the data structures inside the java.lang.Map<K, V>, leading to bugs which manifest rarely or unpredictably, and which are difficult to detect and fix. Also, concurrent modification by one thread with read access by another thread or threads will sometimes give unpredictable results to the reader, although the map's internal consistency will not be destroyed. Using external program logic to prevent concurrent modification increases code complexity and creates an unpredictable risk of errors in existing and future code, although it enables non-concurrent Collections to be used. However, either locks or program logic cannot coordinate external threads which may come in contact with the java.util.Collection<E>.

Modification counters In order to help with the concurrent modification problem, the non-concurrent java.lang.Map<K, V> implementations and other java.util.Collection<E>s use internal modification counters which are consulted before and after a read to watch for changes: the writers increment the modification counters. A concurrent modification is supposed to be detected by this mechanism, throwing a java.util.ConcurrentModificationException, but it is not guaranteed to occur in all cases and should not be relied on. The counter maintenance is also a performance reducer. For performance reasons, the counters are not volatile, so it is not guaranteed that changes to them will be propagated between Threads.

Collections.synchronizedMap() One solution to the concurrent modification problem is using a particular wrapper class provided by a factory in java.util.Collections : public static <K, V> Map<K, V> synchronizedMap(Map<K, V> m) which wraps an existing non-thread-safe Map with methods that synchronize on an internal mutex. There are also wrappers for the other kinds of java.util.Collection<E>s. This is a partial solution, because it is still possible that the underlying java.util.Map<K, V> can be inadvertently accessed by java.lang.Threads which keep or obtain unwrapped references. Also, all java.util.Collection<E>s implement the java.lang.Iterable but the synchronized-wrapped java.util.Map<K, V>s and other wrapped java.util.Collection<E>s do not provide synchronized iterators, so the synchronization is left to the client code, which is slow and error prone and not possible to expect to be duplicated by other consumers of the synchronized java.util.Map<K, V>. The entire duration of the iteration must be protected as well. Furthermore, a java.util.Map<K, V> which is wrapped twice in different places will have different internal mutex objects on which the synchronizations operate, allowing overlap. The delegation is a performance reducer, but modern just-in-time compilers often inline heavily, limiting the performance reduction. Here is how the wrapping works inside the wrapper - the mutex is just a final java.util.Object and m is the final wrapped java.util.Map<K, V>:

The synchronization of the iteration is recommended as follows; however, this synchronizes on the wrapper rather than on the internal mutex, allowing overlap:

… excerpt ends here. Continue reading the full article.

Illustrations

Java ConcurrentMap illustration
Java ConcurrentMap illustration

Worked examples

Example 1 — a first encounter with Java ConcurrentMap

Start with the simplest possible case. Write down what Java ConcurrentMap claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java ConcurrentMap before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java ConcurrentMap ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java ConcurrentMap

In research
Java ConcurrentMap appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java ConcurrentMap in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java ConcurrentMap is common in secondary-school and first-year university syllabi. It links to neighbouring topics Distributed data structures, JDK components, so understanding it makes those chapters shorter.
In everyday life
Look for Java ConcurrentMap outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Java ConcurrentMap” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Java ConcurrentMap in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java ConcurrentMap means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java ConcurrentMap out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java ConcurrentMap in simple terms?

The Java programming language's Java Collections Framework version 1.5 and later defines and implements the original regular single-threaded maps, and also new thread-safe maps implementing the java.util.concurrent.ConcurrentMap interface among other concurrent interfaces. In Java 1.6, the java.uti…

Why does Java ConcurrentMap matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java ConcurrentMap?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java ConcurrentMap.

Tags

  • Distributed data structures
  • JDK components

Keep exploring