diff -r 376bb53438b7 -r 415a5242e046 jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java --- a/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java Mon May 11 17:54:03 2015 -0700 +++ b/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java Tue May 12 10:50:40 2015 +0200 @@ -34,6 +34,7 @@ */ package java.util.concurrent; + import java.util.Map; import java.util.Objects; import java.util.function.BiConsumer; @@ -44,6 +45,14 @@ * A {@link java.util.Map} providing thread safety and atomicity * guarantees. * + *
To maintain the specified guarantees, default implementations of + * methods including {@link #putIfAbsent} inherited from {@link Map} + * must be overridden by implementations of this interface. Similarly, + * implementations of the collections returned by methods {@link + * #keySet}, {@link #values}, and {@link #entrySet} must override + * methods such as {@code removeIf} when necessary to + * preserve atomicity guarantees. + * *
Memory consistency effects: As with other concurrent
* collections, actions in a thread prior to placing an object into a
* {@code ConcurrentMap} as a key or value
@@ -60,7 +69,7 @@
* @param The default implementation is equivalent to, for this {@code map}:
* The default implementation may retry these steps when multiple
* threads attempt updates including potentially calling the remapping
{@code
- * for ((Map.Entry
+ * for (Map.Entry {@code
+ * with a value, associates it with the given value.
+ * This is equivalent to, for this {@code map}:
+ *
*
* except that the action is performed atomically.
*
@@ -147,18 +155,19 @@
* @throws IllegalArgumentException if some property of the specified key
* or value prevents it from being stored in this map
*/
- V putIfAbsent(K key, V value);
+ V putIfAbsent(K key, V value);
/**
* Removes the entry for a key only if currently mapped to a given value.
- * This is equivalent to
- * {@code
* if (!map.containsKey(key))
* return map.put(key, value);
* else
- * return map.get(key);
- * }
+ * return map.get(key);} {@code
- * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
+ * This is equivalent to, for this {@code map}:
+ *
*
* except that the action is performed atomically.
*
@@ -181,14 +190,15 @@
/**
* Replaces the entry for a key only if currently mapped to a given value.
- * This is equivalent to
- * {@code
+ * if (map.containsKey(key)
+ * && Objects.equals(map.get(key), value)) {
* map.remove(key);
* return true;
- * } else
+ * } else {
* return false;
- * }
+ * }} {@code
- * if (map.containsKey(key) && Objects.equals(map.get(key), oldValue)) {
+ * This is equivalent to, for this {@code map}:
+ *
*
* except that the action is performed atomically.
*
@@ -212,13 +222,12 @@
/**
* Replaces the entry for a key only if currently mapped to some value.
- * This is equivalent to
- * {@code
+ * if (map.containsKey(key)
+ * && Objects.equals(map.get(key), oldValue)) {
* map.put(key, newValue);
* return true;
- * } else
+ * } else {
* return false;
- * }
+ * }} {@code
- * if (map.containsKey(key)) {
+ * This is equivalent to, for this {@code map}:
+ *
*
* except that the action is performed atomically.
*
@@ -249,12 +258,14 @@
* @implSpec
* {@code
+ * if (map.containsKey(key))
* return map.put(key, value);
- * } else
- * return null;
- * }
+ * else
+ * return null;} {@code
- * for ((Map.Entry
+ * for (Map.Entry {@code
* if (map.get(key) == null) {
- * V newValue = mappingFunction.apply(key);
- * if (newValue != null)
- * return map.putIfAbsent(key, newValue);
- * }
- * }
+ * V newValue = mappingFunction.apply(key);
+ * if (newValue != null)
+ * return map.putIfAbsent(key, newValue);
+ * }}
*
* The default implementation may retry these steps when multiple
* threads attempt updates including potentially calling the mapping
@@ -331,18 +341,17 @@
* @implSpec
* The default implementation is equivalent to performing the following
* steps for this {@code map}, then returning the current value or
- * {@code null} if now absent. :
+ * {@code null} if now absent:
*
* {@code
* if (map.get(key) != null) {
- * V oldValue = map.get(key);
- * V newValue = remappingFunction.apply(key, oldValue);
- * if (newValue != null)
- * map.replace(key, oldValue, newValue);
- * else
- * map.remove(key, oldValue);
- * }
- * }
+ * V oldValue = map.get(key);
+ * V newValue = remappingFunction.apply(key, oldValue);
+ * if (newValue != null)
+ * map.replace(key, oldValue, newValue);
+ * else
+ * map.remove(key, oldValue);
+ * }}
*
* The default implementation may retry these steps when multiple threads
* attempt updates including potentially calling the remapping function
@@ -363,13 +372,13 @@
BiFunction super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue;
- while((oldValue = get(key)) != null) {
+ while ((oldValue = get(key)) != null) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null) {
if (replace(key, oldValue, newValue))
return newValue;
} else if (remove(key, oldValue))
- return null;
+ return null;
}
return oldValue;
}
@@ -386,17 +395,16 @@
* V oldValue = map.get(key);
* V newValue = remappingFunction.apply(key, oldValue);
* if (oldValue != null ) {
- * if (newValue != null)
- * map.replace(key, oldValue, newValue);
- * else
- * map.remove(key, oldValue);
+ * if (newValue != null)
+ * map.replace(key, oldValue, newValue);
+ * else
+ * map.remove(key, oldValue);
* } else {
- * if (newValue != null)
- * map.putIfAbsent(key, newValue);
- * else
- * return null;
- * }
- * }
+ * if (newValue != null)
+ * map.putIfAbsent(key, newValue);
+ * else
+ * return null;
+ * }}
*
* The default implementation may retry these steps when multiple
* threads attempt updates including potentially calling the remapping
@@ -417,7 +425,7 @@
BiFunction super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue = get(key);
- for(;;) {
+ for (;;) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue == null) {
// delete mapping
@@ -458,7 +466,6 @@
}
}
-
/**
* {@inheritDoc}
*
@@ -470,12 +477,11 @@
* {@code
* V oldValue = map.get(key);
* V newValue = (oldValue == null) ? value :
- * remappingFunction.apply(oldValue, value);
+ * remappingFunction.apply(oldValue, value);
* if (newValue == null)
- * map.remove(key);
+ * map.remove(key);
* else
- * map.put(key, newValue);
- * }
+ * map.put(key, newValue);}
*
*