Java8 - 自定义实现体会CompletableFuture的原理

文章目录


Java8 - 自定义实现体会CompletableFuture的原理


Code

Future 接口 的局限性有很多,其中一个就是需要主动的去询问是否完成,如果等子线程的任务完成以后,通知我,那岂不是更好?

public class FutureInAction3 {


    public static void main(String[] args) {


        Future<String> future = invoke(() -> {
            try {
                Thread.sleep(10000L);
                return "I am Finished.";
            } catch (InterruptedException e) {
                return "I am Error";
            }
        });


        future.setCompletable(new Completable<String>() {
            @Override
            public void complete(String s) {
                System.out.println("complete called ---- " + s);
            }

            @Override
            public void exception(Throwable cause) {
                System.out.println("error");
                cause.printStackTrace();
            }
        });
        System.out.println("....do something else .....");
        System.out.println("try to get result ->" + future.get());
    }

    private static <T> Future<T> invoke(Callable<T> callable) {

        AtomicReference<T> result = new AtomicReference<>();
        AtomicBoolean finished = new AtomicBoolean(false);

        Future<T> future = new Future<T>() {
            private Completable<T> completable;

            @Override
            public T get() {
                return result.get();
            }

            @Override
            public boolean isDone() {
                return finished.get();
            }

            // 设置完成
            @Override
            public void setCompletable(Completable<T> completable) {
                this.completable = completable;
            }

            // 获取
            @Override
            public Completable<T> getCompletable() {
                return completable;
            }
        };
        Thread t = new Thread(() -> {
            try {
                T value = callable.action();
                result.set(value);
                finished.set(true);
                if (future.getCompletable() != null)
                    future.getCompletable().complete(value);
            } catch (Throwable cause) {
                if (future.getCompletable() != null)
                    future.getCompletable().exception(cause);
            }
        });
        t.start();

        return future;
    }


    private interface Future<T> {

        T get();

        boolean isDone();

        //  1
        void setCompletable(Completable<T> completable);

        //  2
        Completable<T> getCompletable();
    }

    private interface Callable<T> {
        T action();
    }

    // 回调接口
    private interface Completable<T> {

        void complete(T t);

        void exception(Throwable cause);
    }
}

Java8 - 自定义实现体会CompletableFuture的原理

上一篇:SharpGL(46)用Billboard绘制头顶文字


下一篇:Java8 - 使用工厂方法 supplyAsync创建 CompletableFuture