美文网首页
java异步之CompletableFuture

java异步之CompletableFuture

作者: 爱吃苹果的西瓜 | 来源:发表于2019-09-15 22:16 被阅读0次

    异步一般用来处理耗时非常多的计算,如果你的计算量不是很大,调用异步方法反而没有执行来的快,我在这里为大家简单的整理一下异步的知识以及用法,我写了一个Main的类,大家可以跑其中的一个方法,把其他的注释掉,这样就可以对异步有一个大致的了解了。

    方法 入参 返回值
    runAsync Runnable Void
    thenAccept T void
    thenApply T U
    thenRun Runnable Void
    supplyAsync T U
    /**
     * main
     *
     * @author 719383495@qq.com |719383495qq@gmail.com |gfu
     * @date 2019/8/30
     */
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
            Async async = new Async();
            async.doAsync0();
            async.doAsync1();
            async.doAsync2();
            async.doAsync3();
            async.doAsync4();
            Thread.sleep(6000);
        }
    }
    
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    
    /**
     * async
     *
     * @author 719383495@qq.com |719383495qq@gmail.com |gfu
     * @date 2019/8/30
     */
    public class Async {
    
        static final int NINE_MULTI_BY_NINE_LENGTH = 10;
    
        public long print() {
            System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":");
            long start0 = System.nanoTime();
            System.out.println("----------start---------");
            for (int i = 0; i < NINE_MULTI_BY_NINE_LENGTH; i++) {
                for (int j = 0; j < NINE_MULTI_BY_NINE_LENGTH; j++) {
                    System.out.print(i * j + " ");
                }
                System.out.println();
            }
            long end0 = System.nanoTime();
            System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + (end0 - start0));
            return end0 - start0;
        }
    
        public void doAsync0() {
            CompletableFuture.runAsync(this::print);
        }
    
        /**
         * need return value
         */
        public void doAsync1() {
            CompletableFuture.supplyAsync(() -> print());
        }
    
        /**
         * you can see it's method to judge whether must need params or return
         * thenAccept can accept pre-stage's result as params
         * thenAccept and thenApply must need params
         * thenAccept can return void but thenApply need to have a return value
         */
        public void doAsync2() {
            CompletableFuture.supplyAsync(() -> print())
                    .thenAccept((i) -> {
                        System.out.println("-----------------------------------------------");
                        System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + "#" + i);
                    });
        }
    
        /**
         * complete and CompletableFuture's get method can let you get it.s async calculate result
         */
        public void doAsync3() {
            CompletableFuture cf = new CompletableFuture();
            cf.complete("hello");
            try {
                System.out.println(cf.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    
        public void doAsync4() {
            CompletableFuture cf0 = CompletableFuture.supplyAsync(() -> print());
            CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> "world");
            CompletableFuture cf2 = CompletableFuture.supplyAsync(() -> "java8");
            CompletableFuture cf = CompletableFuture.allOf(cf0);
            try {
                System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf0.get());
                System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf1.get());
                System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf2.get());
                System.out.println(cf.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    
    
    }
    

    相关文章

      网友评论

          本文标题:java异步之CompletableFuture

          本文链接:https://www.haomeiwen.com/subject/aconectx.html