前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java异步之CompletableFuture

java异步之CompletableFuture

作者头像
gfu
发布2019-09-18 11:42:40
8780
发布2019-09-18 11:42:40
举报
文章被收录于专栏:gfu

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

方法

入参

返回值

runAsync

Runnable

Void

thenAccept

T

void

thenApply

T

U

thenRun

Runnable

Void

supplyAsync

T

U

代码语言:javascript
复制
/**
 * 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);
    }
}
代码语言:javascript
复制
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();
        }
    }


}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.09.15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档