为什么将来可完成的代码比并行流慢?

问题描述 投票:2回答:2

我有一段代码。该代码用于学习CompletableFuture

package com.test.omn.hello;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class CompletableFutureLearning {

    public static void main(String[] args) {

        List<Shop> shops = new ArrayList<>();

        shops.add(new Shop("Videocon Tv", "100 $"));
        shops.add(new Shop("Videocon Tv", "200 $"));
        shops.add(new Shop("Videocon Tv", "300 $"));
        shops.add(new Shop("Videocon Tv", "400 $"));
        long start_time;
        long end_time;
        double difference;


        System.out.println("parallel stream");
        start_time = System.nanoTime();
        shops.parallelStream().forEach(e -> System.out.println(e.getPrice()));
        end_time = System.nanoTime();
        difference = (end_time - start_time) / 1e6;
        System.out.println("execution time " + difference);

        System.out.println("completable futures stream");
        start_time = System.nanoTime();

        List<CompletableFuture<String>> result = shops.parallelStream()
                .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice())).collect(Collectors.toList());

        List<String> result1 = result.parallelStream().map(CompletableFuture::join).collect(Collectors.toList());

        result1.forEach(e -> System.out.println(e));
        end_time = System.nanoTime();
        difference = (end_time - start_time) / 1e6;
        System.out.println("execution time " + difference);

    }

    static public class Shop {

        public Shop(String name, String price) {
            super();
            this.name = name;
            this.price = price;
        }

        private String name;

        private String price;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {

            try {
                Thread.sleep(3000l);
            } catch (InterruptedException e) {
            }
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

    }

}

以下是我运行代码时的结果。我可以看到并行流的执行时间总是比CompletableFuture的执行时间快。我希望执行时间大致相同。知道为什么会这样吗?

parallel stream
300 $
400 $
100 $
200 $
execution time 3079.88547
completable futures stream
100 $
200 $
300 $
400 $
execution time 6018.84133
java java-8 java-stream completable-future
2个回答
2
投票

我想在第二个例子中,这里:

List<String> result1 = result.parallelStream().map(CompletableFuture::join).collect(Collectors.toList());

您通过两次单独的线程执行包装代码:两次执行parallelStream,一次,第二次调用CompletableFuture :: join,它正在调用已经异步 CompletableFuture。

考虑按流在第二个扇区中交换parallelStream:

List<String> result1 = result.stream().map(CompletableFuture::join).collect(Collectors.toList());

P.S。在我的机器上,几次运行后的结果几乎相同:

parallel stream
300 $
400 $
200 $
100 $
execution time 3007.854272
completable futures stream
100 $
200 $
300 $
400 $
execution time 3006.914028

也许在您的情况下,公共线程池中的线程数量小于情况2所需的线程数量,因此像我所考虑的那样更改代码应该可以解决问题。


1
投票

问题出在线程池中。

以下代码将在线程池中使用x个线程执行。

shops.parallelStream().forEach(e -> System.out.println(e.getPrice()));

以下内容应该已经在线程池中执行了y个线程

  List<CompletableFuture<String>> result = shops.parallelStream()
                    .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice())).collect(Collectors.toList());

            List<String> result1 = result.parallelStream().map(CompletableFuture::join).collect(Collectors.toList());

在我的机器上可能是。 x> y

但是一旦我更改下面的代码,结果将有所不同

package com.test.omn.hello;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

public class CompletableFutureLearning {

    public static void main(String[] args) {

        List<Shop> shops = new ArrayList<>();

        shops.add(new Shop("Videocon Tv", "100 $"));
        shops.add(new Shop("Videocon Tv", "200 $"));
        shops.add(new Shop("Videocon Tv", "300 $"));
        shops.add(new Shop("Videocon Tv", "400 $"));
        shops.add(new Shop("Videocon Tv", "100 $"));
        shops.add(new Shop("Videocon Tv", "200 $"));
        shops.add(new Shop("Videocon Tv", "300 $"));
        shops.add(new Shop("Videocon Tv", "400 $"));
        shops.add(new Shop("Videocon Tv", "300 $"));
        shops.add(new Shop("Videocon Tv", "400 $"));


        long start_time;
        long end_time;
        double difference;
//      System.out.println("sequential stream");
//      
//      long start_time = System.nanoTime();
//      long end_time = System.nanoTime();
//      double difference = (end_time - start_time) / 1e6;
//      System.out.println("execution time "+ difference);



        System.out.println("parallel stream");
        start_time = System.nanoTime();
        shops.parallelStream().forEach(e -> System.out.println(e.getPrice()));
        end_time = System.nanoTime();
        difference = (end_time - start_time) / 1e6;
        System.out.println("execution time " + difference);

        System.out.println("completable futures stream");
        start_time = System.nanoTime();

        ExecutorService threadPool  = Executors.newFixedThreadPool(8);


        List<CompletableFuture<String>> result = shops.stream()
                .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(),threadPool)).collect(Collectors.toList());

        List<String> result1 = result.stream().map(CompletableFuture::join).collect(Collectors.toList());

        result1.forEach(e -> System.out.println(e));
        end_time = System.nanoTime();
        difference = (end_time - start_time) / 1e6;
        System.out.println("execution time " + difference);





    }

    static public class Shop {

        public Shop(String name, String price) {
            super();
            this.name = name;
            this.price = price;
        }

        private String name;

        private String price;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {

            try {
                Thread.sleep(2000l);
            } catch (InterruptedException e) {
            }
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

    }

}

立即结果

parallel stream
300 $
200 $
300 $
300 $
400 $
100 $
100 $
200 $
400 $
400 $
execution time 6093.126747
completable futures stream
100 $
200 $
300 $
400 $
100 $
200 $
300 $
400 $
300 $
400 $
execution time 4022.263999
© www.soinside.com 2019 - 2024. All rights reserved.