③. CompletableFuture API
①. 获得结果和触发计算(get、getNow、join、complete)
- ①. 获得结果和触发计算
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();} return 1; }); //(1).public T get()不见不散(会抛出异常) //System.out.println(future.get()); //(2).public T get(long timeout, TimeUnit unit) 过时不候2s后如果没有返回结果就报错 //System.out.println(future.get(2,TimeUnit.SECONDS)); //public T getNow(T valuelfAbsent) try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();} //没有计算完成的情况下,给我一个替代结果 //Integer now = future.getNow(3); //这里停顿了3s,而我2s后就有结果了,所以可以正常拿到值 false获取到的值是1 //如果这里停顿1s,而我2s后才有结果,那么就不可以正常拿到值,true获取到的值是444 boolean flag = future.complete(444); System.out.println(flag+"获取到的值是"+future.get());
②. 对计算结果进行处理(thenApply、handle)
- ②. 对计算结果进行处理
- whenComplete:是执行当前任务的线程执行继续执行whenComplete的任务
- whenCompleteAsync:是执行把whenCompleteAsync这个任务继续提交给线程池来进行执行
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();} return 1; }).thenApply(s->{ System.out.println("-----1"); //如果加上int error=1/0; 由于存在依赖关系(当前步错,不走下一步),当前步骤有异常的话就叫停 //int error=1/0; return s+1; }).thenApply(s->{ System.out.println("-----2"); return s+2; }).whenComplete((v,e)->{ if(e==null){ System.out.println("result-----"+v); } }).exceptionally(e->{ e.printStackTrace(); return null; }); System.out.println(Thread.currentThread().getName()+"\t"+"over...."); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {e.printStackTrace();}
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 1L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); System.out.println(CompletableFuture.supplyAsync(() -> { return 1; }).handle((f,e) -> { System.out.println("-----1"); return f + 2; }).handle((f,e) -> { System.out.println("-----2"); //如果这里异常了,handle方法依旧可以继续执行下去 /* -----1 -----2 -----3 null java.util.concurrent.CompletionException: java.lang.NullPointerException at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273) at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280) at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:824) at java.util.concurrent.CompletableFuture.uniHandleStage(CompletableFuture.java:834) at java.util.concurrent.CompletableFuture.handle(CompletableFuture.java:2155) at com.atguigu.juc.api.CompletableFutureApiDemo.main(CompletableFutureApiDemo.java:19) Caused by: java.lang.NullPointerException at com.atguigu.juc.api.CompletableFutureApiDemo.lambda$main$3(CompletableFutureApiDemo.java:21) at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822) ... 3 more * */ int error=1/0; return f + 3; }).handle((f,e) -> { System.out.println("-----3"); return f + 4; }).whenComplete((v, e) -> { if (e == null) { System.out.println("----result: " + v); } }).exceptionally(e -> { e.printStackTrace(); return null; }).join()); threadPoolExecutor.shutdown();