Why is CompletableFuture join/get faster in separate streams than using one stream

前端 未结 3 1719
眼角桃花
眼角桃花 2021-01-02 21:51

For the following program I am trying to figure out why using 2 different streams parallelizes the task and using the same stream and calling join/get on the Completable fut

3条回答
  •  渐次进展
    2021-01-02 22:21

    The two approaches are quite different, let me try to explain it clearly

    1st approach : In the first approach you are spinning up all Async requests for all 6 tasks and then calling join function on each one of them to get the result

    2st approach : But in the second approach you are calling the join immediately after spinning the Async request for each task. For example after spinning Async thread for task 1 calling join, make sure that thread to complete task and then only spin up the second task with Async thread

    Note : Another side if you observe the output clearly, In the 1st approach output appears in random order since the all six tasks were executed asynchronously. But during second approach all tasks were executed sequentially one after the another.

    I believe you have an idea how stream map operation is performed, or you can get more information from here or here

    To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

提交回复
热议问题