我有一个重要的数据集,并且想要调用缓慢而干净的方法,并且比第一个调用结果有副作用的快速方法要快。我对中间结果不感兴趣,所以我不想收集它们。在并行流上调用顺序使所有以前的操作顺序
明显的解决方案是创建并行流,进行慢速呼叫,再次使流顺序,并进行快速调用。问题是,在单线程中执行的所有代码都没有实际的并行性。
示例代码:
@Test
public void testParallelStream() throws ExecutionException, InterruptedException
{
ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors() * 2);
Set<String> threads = forkJoinPool.submit(()-> new Random().ints(100).boxed()
.parallel()
.map(this::slowOperation)
.sequential()
.map(Function.identity())//some fast operation, but must be in single thread
.collect(Collectors.toSet())
).get();
System.out.println(threads);
Assert.assertEquals(Runtime.getRuntime().availableProcessors() * 2, threads.size());
}
private String slowOperation(int value)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return Thread.currentThread().getName();
}
如果删除sequential
,代码执行如预期的,但,很明显,不平行的操作将在多个线程中调用。
您能否推荐一些有关此类行为的参考文献,或者可能采用某种方法来避免临时收集?
您引用的句子在Java 8版本中完全相同,可以在类文档的最后一个段落中找到。通常,您可以在[包文档](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps)中找到更多信息(请参阅“并行性” )而不是[特定方法](https://docs.oracle.com/javase/8/docs/api/java/util/stream/BaseStream.html#parallel--),而不仅限于并行/顺序模式(例如与减少量相比)。 – Holger
斑点!我知道它是[更新](http://hg.openjdk.java.net/jdk9/dev/jdk/rev/d52b2d49bf04)(我甚至参加了讨论并[相信](http://mail.openjdk。 java.net/pipermail/core-libs-dev/2015-August/034773.html)Stuart为'concat'添加一个特别的注释),但由于某种原因找到了错误的地方。发布编辑。 –