ConcurrentHashMap parallelismThreshold

Ben*_*ünz 7 java parallel-processing concurrenthashmap java-8

ConcurrentHashMap有一对夫妇的新方法.我有两个问题:

  1. 他们为什么不宣布ConcurrentMap
  2. 究竟是什么parallelismThreshold意思或做什么?

dka*_*zel 8

  1. 这些新方法似乎依赖于ConcurrentHashMap特有的实现细节,但您必须从Java 8作者那里得到答案.(他们确实浏览了SO)

  2. 从ConcurrentHashMap的Javadoc:

    这些批量操作接受parallelismThreshold参数.如果估计当前地图大小小于给定阈值,则方法顺序进行.使用Long.MAX_VALUE值可以抑制所有并行性.使用值1可通过划分为足够的子任务来充分利用用于所有并行计算的ForkJoinPool.commonPool()来实现最大并行度.通常,您最初会选择其中一个极值,然后测量使用中间值的性能,这些值会影响开销与吞吐量之间的关系.


Era*_*ran 5

parallelismThreshold确定是否批量操作将被顺序地或并行地执行。并行运行会有一些开销,因此仅在超出某些地图大小阈值时才有用。

ConcurrentHashMaps支持一组顺序和并行的批量操作,与大多数Stream方法不同,该操作被设计为即使在其他线程同时更新的映射中也可以安全且通常明智地应用。例如,在计算共享注册表中值的快照摘要时。共有三种操作,每种都有四种形式,它们接受带有键,值,条目和(键,值)参数和/或返回值的函数。因为ConcurrentHashMap的元素没有以任何特定的方式排序,并且可以在不同的并行执行中以不同的顺序进行处理,所以提供的函数的正确性不应该依赖于任何顺序,也不应该依赖于任何其他对象或值,而这些对象或值在运行时可能会瞬时改变。计算正在进行中;除了forEach动作外,理想情况下应无副作用。对Map.Entry对象的批量操作不支持方法setValue。

- forEach: Perform a given action on each element. A variant form applies a given
    transformation on each element before performing the action.
- search: Return the first available non-null result of applying a given function
    on each element; skipping further search when a result is found.
- reduce: Accumulate each element. The supplied reduction function cannot rely on
    ordering (more formally, it should be both associative and commutative).
    There are five variants:
    - Plain reductions. (There is not a form of this method for (key, value)
        function arguments since there is no corresponding return type.)
    - Mapped reductions that accumulate the results of a given function applied
        to each element.
    - Reductions to scalar doubles, longs, and ints, using a given basis value.
Run Code Online (Sandbox Code Playgroud)

这些批量操作接受parallelismThreshold参数。如果当前地图的大小估计小于给定的阈值,则方法将按顺序进行。使用Long.MAX_VALUE的值将抑制所有并行性。使用1值可通过划分为足够的子任务来充分利用用于所有并行计算的ForkJoinPool.commonPool()来实现最大的并行度。通常,您最初会选择这些极值之一,然后使用权衡开销与吞吐量的中间值来衡量性能。

来源