CompletableFuture学习

2018-11-08  本文已影响0人  梦想又照进现实
package completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 80374563 on 2018/11/6.
 *
 * https://www.jianshu.com/p/6f3ee90ab7d3
 */
public class CompletableFutureDemo {

    public static void thenRun() {
        CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        }).thenRun(() -> System.out.println("hello world"));
        while (true) {
        }
    }


    /**
     * 结合两个CompletionStage的结果,进行转化后返回
     * <p>
     * <code>
     * public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
     * public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
     * public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);
     * </code>
     */
    public static void thenCombine() {
        String result = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new Shop("zhoudy");
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new Shop("shop");
        }), (s1, s2) -> s1.getName() + " " + s2.getName()).join();
        System.out.println(result);
    }


    /**
     * 结合两个CompletionStage的结果,它需要原来的处理返回值,
     * 并且other代表的CompletionStage也要返回值之后,利用这两个返回值,进行消耗。
     */
    public static void thenAcceptBoth() {
        CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        }).thenAcceptBothAsync(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "world";
        }), (s1, s2) -> System.out.println(s1 + " " + s2)).join();
//        while (true){}
    }

    /**
     * 有两种渠道完成同一个事情,所以就可以调用这个方法,找一个最快的结果进行处理。
     */
    public static void applyToEither() {
        String result = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "s1";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello world";
        }), s -> s).join();
        System.out.println(result);
    }

    static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) {
        long st = System.currentTimeMillis();
//        thenRun();
//        thenCombine();
        thenAcceptBoth();
        System.out.println("done : " + (System.currentTimeMillis() - st) + " msecs");


        //Lambda
//        Runnable r = ()-> {int i=9; System.out.println(i);  };
//        r.run();

    }
}
上一篇下一篇

猜你喜欢

热点阅读