野生程序员 野生程序员

               流年,长短皆逝 浮生,往来皆客。  

目录
异步回调(Juc-09)
/    

异步回调(Juc-09)

简述

异步调用就像是ajax一样可以不用等待直接执行其他的方法

代码

package net.yscxy.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author WangFuKun
 * @create 2020/12/2 9:20
 */
/*
 * 异步调用,类似ajax
 * */
public class Demo01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "supplyAsync=>Integer");

            return 1024;
        });
        System.out.println(completableFuture.whenComplete((u, t) -> {
            System.out.println("u->" + u);
            System.out.println("t->" + t);
        }).exceptionally((e) -> {
            System.out.println( e.getMessage());
            return 233;
        }).get());
    }
}

标题:异步回调(Juc-09)
作者:野生程序员
地址:http://www.yscxy.net/articles/2020/11/24/1606177128072.html