package io.wdd.server;

import java.util.concurrent.CompletableFuture;


public class SimpleTest {

    public static void main(String[] args) {


    }

    private void CompletableFutureTest() {
        //任务1：洗水壶->烧开水
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            try {

                String name = Thread
                        .currentThread()
                        .getName();
                System.out.println("name = " + name);

                System.out.println("T1:洗水壶...");
                Thread.sleep(1000);
                System.out.println("T1:烧开水...");
                Thread.sleep(15000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "热水和水壶";
        });
        //任务2：洗茶壶->洗茶杯->拿茶叶
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("T2:洗茶壶...");
                Thread.sleep(1000);
                System.out.println("T2:洗茶杯...");
                Thread.sleep(2000);
                System.out.println("T2:拿茶叶...");
                Thread.sleep(1000);

                String name = Thread
                        .currentThread()
                        .getName();
                System.out.println("name = " + name);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "龙井";
        });
        //任务3：任务1和任务2完成后执行：泡茶
        CompletableFuture<String> f3 = f1.thenCombine(
                f2,
                (result1, result2) -> {
                    System.out.println("result1 = " + result1);
                    System.out.println("result2 = " + result2);
                    System.out.println("T1:拿到茶叶:" + result2);
                    System.out.println("T1:泡茶...");

                    String currentAThreadName = Thread
                            .currentThread()
                            .getName();
                    System.out.println("currentAThreadName = " + currentAThreadName);

                    return "上茶:" + result2;
                }
        );


        //等待任务3执行结果
        System.out.println(f3.join());
        String currentAThreadName = Thread
                .currentThread()
                .getName();
        System.out.println("currentAThreadName = " + currentAThreadName);
    }
}
