Example #1
0
  @Test
  public void test_findPriceInUSD() {
    Shop s1 = shops.get(0);
    String product = "MacBook 11";

    Future<Double> priceInUSD =
        CompletableFuture.supplyAsync(() -> s.getPrice(product))
            .thenCombine(
                CompletableFuture.supplyAsync(() -> ExchangeService.getRate(USD, FRANC)),
                (price, rate) -> price * rate);
  }
 public Stream<CompletableFuture<String>> findPriceStream(String product) {
   return shops
       .stream()
       .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor))
       .map(future -> future.thenApply(Quote::parse))
       .map(
           future ->
               future.thenCompose(
                   quote ->
                       CompletableFuture.supplyAsync(
                           () -> Discount.applyDiscount(quote), executor)));
 }
 private static void asyncInCompletableFuture() {
   CompletableFuture.supplyAsync(
           () -> {
             for (int i = 0; i < 1000; ++i) {
               if (i % 5 == 0) System.out.println("Doing hard work ....");
             }
             return new File("/tmp/foo");
           })
       .thenApply(File::toPath)
       .thenAccept(System.out::println);
 }