简体   繁体   English

并行流看起来并不像完全并行工作

[英]Parallel stream doesn't look like working in parallel, completely

1. Set's parallelStream doesn't use enough thread. 1. Set的parallelStream没有使用足够的线程。

Java8 parallelStream doesn't working exactly parallel. Java8 parallelStream无法完全并行工作。 In my computer, java8 set's parallelStream is not using enough thread when task's count is smaller than processor's count. 在我的计算机中,当任务计数小于处理器计数时,java8集的parallelStream没有使用足够的线程。

public class ParallelStreamSplitTest {
    @Test
    public void setStreamParallelTest() {
        System.out.printf("Total processor count : %d \n", Runtime.getRuntime().availableProcessors());
        long start = System.currentTimeMillis();
        IntStream.range(1, 8).boxed().collect(Collectors.toCollection(HashSet::new)).parallelStream().forEach((index) -> {
            System.out.println("Starting " + Thread.currentThread().getName() + ",    index=" + index + ", " + new Date());
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        });
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "'s elapsed time : " + (end - start));
    }

    @Test
    public void intStreamParallelTest() {
        System.out.printf("Total processor count : %d \n", Runtime.getRuntime().availableProcessors());
        long start = System.currentTimeMillis();
        IntStream.range(1, 8).parallel().forEach(index -> {
            System.out.println("Starting " + Thread.currentThread().getName() + ",    index=" + index + ", " + new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "'s elapsed time : " + (end - start));
    }
}

In my code, setStreamParallelTest takes 4 seconds whereas intStreamParallelTest takes 1 second. 在我的代码中,setStreamParallelTest花费4秒,而intStreamParallelTest花费1秒。

I expect that setStreamParallelTest also done in 1 seconds. 我希望setStreamParallelTest也可以在1秒内完成。 Is it bug? 是虫子吗?

2. Is it okay to use parallel stream to call another api in web application? 2.可以使用并行流在Web应用程序中调用另一个api吗? If it is wrong, why? 如果不对,为什么?

My web application need to call another api server in parallel. 我的Web应用程序需要并行调用另一个api服务器。 So I use parallel stream to call api. 所以我使用并行流来调用api。

Sets.newHashSet(api1, api2, api3, api4).parallelStream().forEach(api -> callApiSync(api))

I think all requests bound for my server share a fork-join pool. 我认为绑定到我的服务器的所有请求都共享一个fork-join池。 so, It looks dangerous when one of api's response is slow. 因此,当api的响应之一缓慢时,这看起来很危险。

Is it correct? 这是正确的吗?

The contract for parallelStream says: parallelStream的合同说:

Returns a possibly parallel Stream with this collection as its source. 返回与此集合为源的可能并行的Stream。 It is allowable for this method to return a sequential stream. 此方法允许返回顺序流。

If you want to invoke several tasks in parallel, use an ExecutorService . 如果要并行调用多个任务,请使用ExecutorService

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

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