多线程之CompletableFuture(中)
来源:互联网 发布:丢书大作战 知乎 编辑:程序博客网 时间:2024/05/29 19:18
thenAcceptAsync
Consumer 接收上个阶段的返回值T,return Void;返回CompletableFuture<Void>
- thenAccept
该方法是同步的
public static void main(String[] args) { CompletableFuture<Void> stringCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---"); return "hello"; }).thenAccept(s -> { System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync---start---" + s); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync----end---" + s); }); try { System.out.println("============================"); stringCompletableFuture.join(); } catch (Exception e) { e.printStackTrace(); } }
代码结果:
thenAccept方法是mian线程执行的,阻塞了输出“============================”。
- thenAcceptAsync
以Async结尾代表是异步,将上述代码中thenAccept换成thenAcceptAsync,看输出结果:
下面介绍的方法都会有以Async和没有Async结尾两种形式,都是类似的,以Async结尾代表异步,统一只介绍以Async结尾的方法。
thenRunAsync
Runnable 不接受上个阶段返回值和异常,return Void;返回CompletableFuture<Void>
public static void main(String[] args) { CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---"); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---"); return "hello"; }).thenRunAsync(() -> { System.out.println(Thread.currentThread().getName() + "---thenRunAsync---start---"); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---thenRunAsync----end---"); }); try { System.out.println(voidCompletableFuture.join()); } catch (Exception e) { e.printStackTrace(); } }
thenapplyAsync
Function<T, R>
接收上个阶段的返回值T,return R ; 返回CompletableFuture<R>
public static void main(String[] args) { CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> { return "hello"; }).thenApplyAsync(v -> { System.out.println(Thread.currentThread().getName() + "---thenApplyAsync---start---" + v); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---thenApplyAsync----end---" + v); return v.length(); }); try { System.out.println(completableFuture.get()); } catch (Exception e) { e.printStackTrace(); } }
whencompleteAsync
BiConsumer<T, U>
接收上个阶段的返回值T和异常U,return Void;返回上个阶段返回值CompletableFuture<T>
public static void main(String[] args) { CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----------"); return "hello"; }).whenCompleteAsync((V, T) -> { System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync---start---" + V); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync----end---" + V); }); try { System.out.println(stringCompletableFuture.get()); } catch (Exception e) { e.printStackTrace(); } }
handleAsync
BiFunction<T, U, R>
接收上个阶段的返回值T和异常U,return R;返回CompletableFuture<R>
public static void main(String[] args) { CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---"); try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---"); int e = 2/0; return "hello"; }).handleAsync((s, v) -> { System.out.println(Thread.currentThread().getName() + "---handle---start---" + s); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---handle----end---" + s); System.out.println("捕获的异常:"+ v); return s == null ? 0 : s.length(); }); try { System.out.println(completableFuture.get()); } catch (Exception e) { e.printStackTrace(); } }
阅读全文
0 0
- 多线程之CompletableFuture(中)
- 多线程之CompletableFuture(上)
- 多线程之CompletableFuture(下)
- java8新特性(九):CompletableFuture多线程并发异步编程
- CompletableFuture(一)
- CompletableFuture(二)
- CompletableFuture(三)
- CompletableFuture
- CompletableFuture
- CompletableFuture
- CompletableFuture
- Java8的CompletableFuture之二
- Java8新特性之CompletableFuture详解
- Java你应该懂点多线程1-Stream、Executor、CompletableFuture
- java CompletableFuture初探 (并发小实验)
- 疯狂Java学习(86)-----------使用CompletableFuture处理异步超时
- Java学习(86)-----------使用CompletableFuture处理异步超时
- java8-[CompletableFuture]
- CreateToolhelp32Snapshot枚举进程
- WebService(3)——使用免费的天气预报WebService
- Map key contains dots but no replacement was configured! Make sure map keys don't contain dots in th
- associatedtype关联类型
- Java--Servlet、ServletContext与ServletConfig的详解及区别
- 多线程之CompletableFuture(中)
- Android studio导入项目后异常和 升级2.2 之后 Maven插件不能使用解决
- javascript之排序方法
- Simple Spring Flex 官方文档地址
- 欢迎使用CSDN-markdown编辑器
- WebService(4)——WSDL文档解析
- 查看硬盘pn
- uibutton uiview点击不同时响应
- 文章标题