java - Best practice to use ConcurrentMap's putIfAbsent - Stack Overflow
You can use MutableMap.getIfAbsentPut(K, Function0<? extends V>)
from GS Collections.
The advantage over calling get()
, doing a null check, and then calling putIfAbsent()
is that we'll only compute the key's hashCode once, and find the right spot in the hashtable once. In ConcurrentMaps like com.gs.collections.impl.map.mutable.ConcurrentHashMap
, the implementation of getIfAbsentPut()
is also thread-safe and atomic.
import com.gs.collections.impl.map.mutable.ConcurrentHashMap; ... ConcurrentHashMap<String, MyObject> map = new ConcurrentHashMap<>(); map.getIfAbsentPut("key", () -> someExpensiveComputation());
The implementation of com.gs.collections.impl.map.mutable.ConcurrentHashMap
is truly non-blocking. While every effort is made not to call the factory function unnecessarily, there's still a chance it will be called more than once during contention.
This fact sets it apart from Java 8's ConcurrentHashMap.computeIfAbsent(K, Function<? super K,? extends V>)
. The Javadoc for this method states:
The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple...
Read full article from java - Best practice to use ConcurrentMap's putIfAbsent - Stack Overflow
No comments:
Post a Comment