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 — 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? — A handy flowchart to simplify selection of collections 'Which Java Collection to use?' — by Janeve George
Cite this
"Java ConcurrentMap". 1785719012. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1367441563