java异步之CompletableFuture

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

方法入参返回值
runAsyncRunnableVoid
thenAcceptTvoid
thenApplyTU
thenRunRunnableVoid
supplyAsyncTU
/**
 * 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();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的小伙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值