为什么 Hashmap.values().parallelStream() 不能并行运行,而将它们包装在 ArrayList 中可以工作?

2023-12-29

hashmap有两个键值对,它们不是由不同的线程并行处理的。


import java.util.stream.Stream;
import java.util.Map;
import java.util.HashMap;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Map<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.values().parallelStream()
              .peek(x -> System.out.println("processing "+x+" in "+Thread.currentThread()))
              .forEach(System.out::println);
    }
}

Output:

processing 1 in Thread[main,5,main]
1
processing 2 in Thread[main,5,main]
2

URL: https://ideone.com/Hkxkoz https://ideone.com/Hkxkoz

ValueSpliterator 应该尝试将 HashMap 数组拆分为大小为 1 的槽,这意味着两个元素应该在不同的线程中处理。

Source: https://www.codota.com/code/java/methods/java8.util.HMSpliterators https://www.codota.com/code/java/methods/java8.util.HMSpliterators$ValueSpliterator/%3Cinit%3E

将它们包裹起来之后ArrayList,它按预期工作。

        new ArrayList(map.values()).parallelStream()
              .peek(x -> System.out.println("processing "+x+" in "+Thread.currentThread()))
              .forEach(System.out::println);

output:

processing 1 in Thread[ForkJoinPool.commonPool-worker-3,5,main]
1
processing 2 in Thread[main,5,main]
2

正如中所解释的这个答案 https://stackoverflow.com/a/44802784/2711488,这个问题与以下事实有关:HashMap容量可能大于其大小,实际值根据哈希码分布在支持数组上。

对于所有基于数组的分割器,分割逻辑基本相同,无论您是流式传输数组还是流式传输ArrayList, or a HashMap。为了在尽力而为的基础上获得平衡的分割,每次分割将(索引)范围的一半,但在以下情况下HashMap,范围内的实际元素数量与范围大小不同。

原则上,每个基于范围的分割器都可以分割为单个元素,但是,客户端代码(即 Stream API 实现)到目前为止可能无法分割。甚至尝试拆分的决定也是由预期的元素数量和 CPU 核心数量决定的。

采取以下程序

public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    map.put("a", 1);
    map.put("b", 2);

    for(int depth: new int[] { 1, 2, Integer.MAX_VALUE }) {
        System.out.println("With max depth: "+depth);
        Tree<Spliterator<Map.Entry<String, Integer>>> spTree
            = split(map.entrySet().spliterator(), depth);
        Tree<String> valueTree = spTree.map(sp -> "estimated: "+sp.estimateSize()+" "
            +StreamSupport.stream(sp, false).collect(Collectors.toList()));
        System.out.println(valueTree);
    }
}

private static <T> Tree<Spliterator<T>> split(Spliterator<T> sp, int depth) {
    Spliterator<T> prefix = depth-- > 0? sp.trySplit(): null;
    return prefix == null?
        new Tree<>(sp): new Tree<>(null, split(prefix, depth), split(sp, depth));
}

public static class Tree<T> {
    final T value;
    List<Tree<T>> children;

    public Tree(T value) {
        this.value = value;
        children = Collections.emptyList();
    }
    public Tree(T value, Tree<T>... ch) {
        this.value = value;
        children = Arrays.asList(ch);
    }
    public <U> Tree<U> map(Function<? super T, ? extends U> f) {
        Tree<U> t = new Tree<>(value == null? null: f.apply(value));
        if(!children.isEmpty()) {
            t.children = new ArrayList<>(children.size());
            for(Tree<T> ch: children) t.children.add(ch.map(f));
        }
        return t;
    }
    public @Override String toString() {
        if(children.isEmpty()) return value == null? "": value.toString();
        final StringBuilder sb = new StringBuilder(100);
        toString(sb, 0, 0);
        return sb.toString();
    }
    public void toString(StringBuilder sb, int preS, int preEnd) {
        final int myHandle = sb.length() - 2;
        sb.append(value == null? "": value).append('\n');
        final int num = children.size() - 1;
        if (num >= 0) {
            if (num != 0) {
                for (int ix = 0; ix < num; ix++) {
                    int nPreS = sb.length();
                    sb.append(sb, preS, preEnd);
                    sb.append("\u2502 ");
                    int nPreE = sb.length();
                    children.get(ix).toString(sb, nPreS, nPreE);
                }
            }
            int nPreS = sb.length();
            sb.append(sb, preS, preEnd);
            final int lastItemHandle = sb.length();
            sb.append("  ");
            int nPreE = sb.length();
            children.get(num).toString(sb, nPreS, nPreE);
            sb.setCharAt(lastItemHandle, '\u2514');
        }
        if (myHandle > 0) {
            sb.setCharAt(myHandle, '\u251c');
            sb.setCharAt(myHandle + 1, '\u2500');
        }
    }
}

你会得到:

With max depth: 1

├─estimated: 1 [a=1, b=2]
└─estimated: 1 []

With max depth: 2

├─
│ ├─estimated: 0 [a=1, b=2]
│ └─estimated: 0 []
└─
  ├─estimated: 0 []
  └─estimated: 0 []

With max depth: 2147483647

├─
│ ├─
│ │ ├─
│ │ │ ├─estimated: 0 []
│ │ │ └─estimated: 0 [a=1]
│ │ └─
│ │   ├─estimated: 0 [b=2]
│ │   └─estimated: 0 []
│ └─
│   ├─
│   │ ├─estimated: 0 []
│   │ └─estimated: 0 []
│   └─
│     ├─estimated: 0 []
│     └─estimated: 0 []
└─
  ├─
  │ ├─
  │ │ ├─estimated: 0 []
  │ │ └─estimated: 0 []
  │ └─
  │   ├─estimated: 0 []
  │   └─estimated: 0 []
  └─
    ├─
    │ ├─estimated: 0 []
    │ └─estimated: 0 []
    └─
      ├─estimated: 0 []
      └─estimated: 0 []

On ideone https://ideone.com/z3f1g0

因此,如前所述,如果我们分割得足够深,分割器可以分割成单个元素,但是,两个元素的估计大小并不表明值得这样做。在每次分割时,它会将估计值减半,虽然您可能会说这对于您感兴趣的元素来说是错误的,但对于这里的大多数分割器来说实际上是正确的,因为当下降到最大级别时,大多数分割器代表一个空范围事实证明,将它们分开是一种资源浪费。

正如另一个答案中所述,该决定是关于平衡拆分工作(或一般准备工作)和预期并行化工作,而 Stream 实现无法提前知道这一点。如果您事先知道每个元素的工作量将非常高,为了证明更多的准备工作是合理的,您可以使用,例如new ArrayList<>(map.[keySet|entrySet|values]()) .parallelStream()强制平衡分割。通常,无论如何,对于较大的地图,问题会小得多。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Hashmap.values().parallelStream() 不能并行运行,而将它们包装在 ArrayList 中可以工作? 的相关文章

随机推荐