当值已存在时,您可能会遇到争用。
如果你看一下 computeIfAbsent() 的源代码,它非常复杂,但你会看到检查值是否已经存在,就在同步块内。考虑这个替代版本(不以原子方式运行):
/**
* Alternate implementation that doesn't block when map already
* contains the value
*/
public V computeIfAbsent2(K key, Function<? super K, ? extends V> mappingFunction) {
V value = get(key);
if (value == null) {
value = mappingFunction.apply(key);
put(key, value);
}
return value;
}
我运行了一个JMH测试,将此替代实现与原始实现进行比较。我运行了20个线程,并使用了包含20个已存在值的 ConcurrentHashMap。每个线程将使用所有 20 个值。测试仅执行该值已存在的情况。它在OS X上运行。结果(2分钟热身后)是
Benchmark Mode Cnt Score Error Units
ComputIfAbsentTest.benchComputeAbsent thrpt 2 77966.354 ops/ms
ComputIfAbsentTest.benchComputeAbsent2 thrpt 2 463096.033 ops/ms
我还尝试在启用飞行录制的情况下运行此功能,并且争用清晰可见。下面是一个示例堆栈跟踪:
"local.ComputIfAbsentTest.benchComputeAbsent-jmh-worker-11" #25 daemon prio=5 os_prio=31 tid=0x00007f89da10b000 nid=0x7203 waiting for monitor entry [0x00007000021f8000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1674)
- waiting to lock <0x0000000795f80540> (a java.util.concurrent.ConcurrentHashMap$Node)
at local.ComputIfAbsentTest.benchComputeAbsent(ComputIfAbsentTest.java:87)
at local.generated.ComputIfAbsentTest_benchComputeAbsent_jmhTest.benchComputeAbsent_thrpt_jmhStub(ComputIfAbsentTest_benchComputeAbsent_jmhTest.java:116)
at local.generated.ComputIfAbsentTest_benchComputeAbsent_jmhTest.benchComputeAbsent_Throughput(ComputIfAbsentTest_benchComputeAbsent_jmhTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:430)
at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:412)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)