encyclopegia.org

Java ConcurrentMap

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 interface among other concurrent interfaces. In Java 1.6, the interface was added, extending , and the interface was added as a subinterface combination.

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 interface among other concurrent interfaces. In Java 1.6, the interface was added, extending , and the interface was added as a subinterface combination. public V put(K key, V value) { synchronized (mutex) { return m.put(key, value); } } The synchronization of the iteration is recommended as follows; however, this synchronizes on the wrapper rather than on the internal mutex, allowing overlap: import java.util.Collections; import java.util.Map; Map wrappedMap = Collections.synchronizedMap(map); synchronized (wrappedMap) { for (String s : wrappedMap.keySet()) { // some possibly long operation executed possibly // many times, delaying all other accesses } } import java.util.HashMap; import java.util.Map; Map map = new HashMap<>(); // Thread A // Use the map itself as the lock. Any agreed object can be used instead. synchronized (map) { map.put("key","value"); } // Thread B synchronized (map) { String result = map.get("key"); // ... } // Thread C synchronized (map) { for (Map.Entry s : map.entrySet()) { /* * Some possibly slow operation, delaying all other supposedly fast operations. * Synchronization on individual iterations is not possible. */ // ... } } import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final ReadWriteLock readLock = lock.readLock(); final ReadWriteLock writeLock = lock.writeLock(); // Thread A try { writeLock.lock(); map.put("key","value"); } finally { writeLock.unlock(); } // Thread B try { readLock.lock(); String s = map.get("key"); } finally { readLock.unlock(); } // Thread C try { readLock.lock(); for (Map.Entry s : map.entrySet()) { /* * Some possibly slow operation, delaying all other supposedly fast operations. * Synchronization on individual iterations is not possible. */ // ... } } finally { readLock.unlock(); } if (k == null || v == null) { throw new NullPointerException(); } if (!m.containsKey(k)) { return m.put(k, v); } else { return m.get(k); } m.replace(k, v) is atomic but equivalent to: if (k == null || v == null) { throw new NullPointerException(); } if (m.containsKey(k)) { return m.put(k, v); } else { return null; } m.replace(k, v1, v2) is atomic but equivalent to: if (k == null || v1 == null || v2 == null) { throw new NullPointerException(); } if (m.containsKey(k) && Objects.equals(m.get(k), v1)) { m.put(k, v2); return true; } else return false; } m.remove(k, v) is atomic but equivalent to: // if Map does not support null keys or values (apparently independently) if (k == null || v == null) { throw new NullPointerException(); } if (m.containsKey(k) && Objects.equals(m.get(k), v)) { m.remove(k); return true; } else return false; } import java.util.concurrent.ConcurrentMap; static final long N = 10; void atomicMultiply(ConcurrentMap map, long key) { while (true) { Long oldValue = map.get(key); // Assuming oldValue is not null. This is the 'payload' operation, and should not have side-effects due to possible re-calculation on conflict Long newValue = oldValue * N; if (map.replace(key, oldValue, newValue)) { break; } } } The putIfAbsent(k, v) is also useful when the entry for the key is allowed to be absent. This example could be implemented with the Java 8 compute() but it shows the overall lock-free pattern, which is more general. The replace(k, v1, v2) does not accept null parameters, so sometimes a combination of them is necessary. In other words, if v1 is null, then putIfAbsent(k, v2) is invoked, otherwise replace(k, v1, v2) is invoked. import java.util.concurrent.ConcurrentMap; void atomicMultiplyNullable(ConcurrentMap map, long key) { while (true) { long oldValue = map.get(key); // This is the 'payload' operation, and should not have side-effects due to possible re-calculation on conflict long newValue = oldValue == null ? INITIAL_VALUE : oldValue * N; if (replaceNullable(map, key, oldValue, newValue)) { break; } } } static boolean replaceNullable(ConcurrentMap map, long key, long v1, long v2) { return v1 == null ? map.putIfAbsent(key, v2) == null : map.replace(key, v1, v2); } ==See also== Java Collections Framework Container (data structure) Java concurrency Lock free ==Citations== ==References== ==External links== Collections Lessons Java 6 Collection Tutorial &mdash; By Jakob Jenkov, Kadafi Kamphulusa Taming Tiger: The Collections Framework 'The Collections Framework' (Oracle Java SE 8 documentation) 'The Java Tutorials - Collections' by Josh Bloch What Java Collection should I use? &mdash; A handy flowchart to simplify selection of collections 'Which Java Collection to use?' &mdash; by Janeve George

References

No reference rows published for this article.

Cite this article

MLA

"Java ConcurrentMap." encyclopegia.org, 2026, https://encyclopegia.org/wiki/java-concurrentmap. Accessed 23 November 2026.

Chicago

"Java ConcurrentMap." encyclopegia.org. Accessed November 23, 2026. https://encyclopegia.org/wiki/java-concurrentmap.

APA

encyclopegia.org. (2026). Java ConcurrentMap. Retrieved November 23, 2026, from https://encyclopegia.org/wiki/java-concurrentmap

Accessed November 23, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Odites diacentra

enwiki · references available

Odites fessa

enwiki · references available

Odites incusata

enwiki · references available

Ade Ojeikere

enwiki · references available

Odites superscripta

enwiki · references available

Odites brachyclista

enwiki · references available

Yi Qiu

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1367441563 · 0.5 · snapshot enwiki-001367441563

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.