package Threads.Test10; import com.sun.xml.internal.ws.util.CompletedFuture; import java.util.concurrent.CompletableFuture; public class CompletableFutureTests { public static void main(String[] args) throws InterruptedException { CompletableFuture<String> completedFuture = CompletableFuture.supplyAsync(CompletableFutureTests::getCode); CompletableFuture<Double> completableFuture = completedFuture.thenApplyAsync(CompletableFutureTests::getPrice); completableFuture.thenAccept((result)->{ System.out.println("获取到股票价格为:"+(double)result); }); completableFuture.exceptionally((e)->{ e.printStackTrace(); return null; }); Thread.sleep(100); } public static String getCode(){ return (long)(Math.random()*1000)+""; } public static double getPrice(String Code){ if(Math.random() <0.4){ throw new RuntimeException("股票代码未查询到"); } return Math.random()*100; } }
Sign in to make a reply
用户6305896977