简体   繁体   English

Java 并行流处理时间不匹配

[英]Java parallel Streams processing time mismatch

On my windows machine, I have 6 cores.在我的 windows 机器上,我有 6 个内核。

I create a stream of 24 elements, and execute an operation on each element using map operation in Stream API. I make use of parallelism:我创建了一个包含 24 个元素的 stream,并在 Stream API 中使用 map 操作对每个元素执行一个操作。我利用了并行性:

myCollection.parallelStream()
                    .map(element -> someTimeConsumingOperation(element)
                    .collect(toList());

someTimeConsumingOperation simulates long operation using: someTimeConsumingOperation 使用以下方法模拟长时间操作:

Thread.sleep(2000);

myCollection has 24 elements. myCollection 有 24 个元素。 I understand that streams use default ForkJoinPool executor to run operations in parallel, which in turn takes into account number of cores to determine how many threads to have in the pool.我知道流使用默认的 ForkJoinPool 执行器来并行运行操作,这反过来又考虑了核心数来确定池中有多少线程。

As there are 6 cores, so I assume max 6 threads are created.因为有 6 个内核,所以我假设最多创建 6 个线程。 If that is the case, and every operation takes almost 2 seconds, entire stream processing must take around 8 seconds(each thread would execute 4 operations as we have 24 elements to process, hence 4*2 seconds).如果是这种情况,并且每个操作需要将近 2 秒,那么整个 stream 处理必须花费大约 8 秒(每个线程将执行 4 个操作,因为我们有 24 个元素要处理,因此需要 4*2 秒)。

But I observe that processing takes just 4 seconds.但我观察到处理只需要 4 秒。

Your 6-core Intel CPU has HyperThreading enabled.您的 6 核 Intel CPU 启用了超线程 With HT, the operating system and applications see each CPU core as 2 separate "logical cores" or "threads" (the nomenclature varies).使用 HT,操作系统和应用程序将每个 CPU 核心视为 2 个独立的“逻辑核心”或“线程”(术语各不相同)。 This means ForkJoinPool will use 12 threads instead of 6.这意味着ForkJoinPool将使用 12 个线程而不是 6 个。

The "logical cores" share some resources between each other, so running 12 tasks in parallel does not give you twice the throughput as running 6 tasks. “逻辑核心”彼此共享一些资源,因此并行运行 12 个任务不会使您的吞吐量是运行 6 个任务的两倍。 But a program that only calls sleep() won't know the difference.但是只调用sleep()的程序不会知道其中的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM