多线程之CompletableFuture(下)
来源:互联网 发布:淘宝商品名称 编辑:程序博客网 时间:2024/06/06 15:48
thenAcceptBothAsync
thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
同时执行两个线程,获取两个返回结果T,U作为参数,进入BiConsumer<T, U>,retrun Void
,返回 CompletableFuture<Void>
public static void main(String[] args) { CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello"; }).thenAcceptBothAsync(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---"); return "hello".length(); }), (t, v) -> { try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "........." + t + "---" + v); }); System.out.println("---------------------------"); voidCompletableFuture.join(); }
acceptEitherAsync
acceptEitherAsync( CompletionStage<? extends T> other, Consumer<? super T> action)
获取快的线程的结果(两个线程返回类型需一致)T,T作为参数,进入Consumer,return void,返回CompletableFuture<Void>
public static void main(String[] args) { CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello1"; }).acceptEitherAsync(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---"); return "hello2"; }), t -> { try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "........." + t); }); System.out.println("---------------------------"); voidCompletableFuture.join(); }
runAfterBothAsync
runAfterBothAsync(CompletionStage<?> other, Runnable action)
两个线程执行完毕后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>
public static void main(String[] args) { CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello1"; }).runAfterBothAsync(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---"); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---"); return "hello2".length(); }), () -> { System.out.println(Thread.currentThread().getName() + "........."); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "........."); }); System.out.println("---------------------------"); voidCompletableFuture.join(); }
runAfterEitherAsync
CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)
有线程结束后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>
public static void main(String[] args) { CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello1"; }).runAfterEitherAsync(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---"); return "hello2".length(); }), () -> { System.out.println(Thread.currentThread().getName() + "........."); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "........."); }); System.out.println("---------------------------"); voidCompletableFuture.join(); }
thenCombineAsync
CompletableFuture<V> thenCombineAsync( CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V>
同时执行两个线程,获取两个返回结果T,U作为参数,进入BiFunction,return v , CompletableFuture<V>
public static void main(String[] args) { CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello1"; }).thenCombineAsync(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---"); System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---"); return 100; }), (t, u) -> { System.out.println(Thread.currentThread().getName() + "........." + t); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "........." + u); return "=thenCombine="; }); try { System.out.println("---------------------------" + completableFuture.get()); } catch (Exception e) { e.printStackTrace(); } }
thenComposeAsync
CompletableFuture<U> thenComposeAsync( Function<? super T, ? extends CompletionStage<U>> fn)
public static void main(String[] args) { CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---"); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---"); return "hello1"; }).thenComposeAsync(t -> { CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---" + t); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---" + t); return 100; }); return integerCompletableFuture; }); try { System.out.println("=----------------------------="); System.out.println("---------------------------" + completableFuture.get()); } catch (Exception e) { e.printStackTrace(); } }
其它方法
- getNow(T valueIfAbsent):线程执行完了,获取返回的值,如果没有执行完毕,返回指定值。不阻塞。
public static void main(String[] args) { CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis()); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis()); return "hello"; }); //执行完了 获取返回的值,如果没有执行完毕,返回指定值 String now = stringCompletableFuture.getNow("i am getNow value"); System.err.println(Thread.currentThread().getName() + "=====getNow值:" + now + " ====时间: " + System.currentTimeMillis()); try { System.err.println(Thread.currentThread().getName() + "====get值:" + stringCompletableFuture.get() + "===时间: " + System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } }
- complete(T value):若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值。
public static void main(String[] args) { CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis()); try { TimeUnit.SECONDS.sleep(4); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis()); return "hello"; }); try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } //若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值 boolean complete = stringCompletableFuture.complete("WWWWW"); System.out.println(complete); try { System.out.println(Thread.currentThread().getName() + " get值:" + stringCompletableFuture.get() + " 时间: " + System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } }
- completeExceptionally(Throwable ex)://若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时抛出指定异常
阅读全文
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初探 (并发小实验)
- (4)Java多线程之安全问题-下
- 多线程之NSOperation(下)
- 疯狂Java学习(86)-----------使用CompletableFuture处理异步超时
- elasticsearch 5.5.0 java操作(新增索引、分页搜索)
- Android Token过期解决方案
- 用markdown使你的博文炫酷起来
- Java
- vue.js添加一些触摸事件以及安装fastclick
- 多线程之CompletableFuture(下)
- 每日练习2017-11-07
- 嵌入式linux的mdev机制
- 第四章 串的基本操作【数据结构】
- 插入排序之折半插入排序
- Beautiful Soup 4.2.0 文档
- POS主密钥与工作密钥关联详解
- java-next(),nextLine()区别
- HttpClients Post请求