Skip to content

Commit

Permalink
Add Javadoc to ConcurrencyUtil.getOrPutIfAbsent (hazelcast#25565)
Browse files Browse the repository at this point in the history
There was [a
query](https://hazelcast.slack.com/archives/C01JU7ZJYGP/p1695899302905289)
about the application of this method - it would be helpful if this was
documented.
  • Loading branch information
JackPGreen authored Sep 29, 2023
1 parent 9a40258 commit b6c98d9
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ public static <K, V> V getOrPutSynchronized(ConcurrentMap<K, V> map, K key, Cont
return value;
}

/**
* Returns the {@code value} corresponding to {@code key} in the {@code map}. If {@code key} is not mapped in {@link map},
* then a {@code value} is computed using {@code func}, inserted into the map and returned.
* <p>
* The behavior is equivalent to {@link ConcurrentMap#computeIfAbsent(K, Function)}, with the following exceptions:
* <ul>
* <li>If no mapping, the value of {@code func.createNew(K)} will be inserted into the {@link map} - even if {@code null}
* <li>Instances of {@link ConcurrentMap} can override their implementation, but here the implementation can be assured
* </ul>
* <p>
* The typical use case of this function over {@link ConcurrentMap#computeIfAbsent(K, Function)} would be in the case of a
* {@link ConcurrentHashMap}, where the implementation is overridden with the following guarantee:<br>
* "The supplied function is invoked exactly once per invocation of this method if the key is absent, else not at all. Some
* attempted update operations on this map by other threads may be blocked while computation is in progress"
* <p>
* By comparison, this implementation cannot guarantee {@code func.createNew(K)} will be executed a maximum of once, because
* no blocking if performed - if the {@code key} is absent from the {@code map}, and this method is called concurrently,
* {@code func.createNew(K)} may be invoked multiple times before any new value is inserted.
* <p>
* This is a performance tradeoff - improve {@code map} throughput and reduce deadlocks, at the expense of redundant object
* instantiation.
*/
public static <K, V> V getOrPutIfAbsent(ConcurrentMap<K, V> map, K key, ConstructorFunction<K, V> func) {
V value = map.get(key);
if (value == null) {
Expand Down

0 comments on commit b6c98d9

Please sign in to comment.