|
| 1 | +package com.example.concurrency.features.completablefuture; |
| 2 | + |
| 3 | +import com.example.concurrency.util.ThreadUtil; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | + |
| 8 | +/** |
| 9 | + * 描述: |
| 10 | + * CompletableFutureExample Java 8 异步编程可描述串行关系、AND汇聚关系和OR汇聚关系以及异常处理 |
| 11 | + * |
| 12 | + * @author zed |
| 13 | + * @since 2019-07-03 1:57 PM |
| 14 | + */ |
| 15 | +public class CompletableFutureExample { |
| 16 | + /** |
| 17 | + * 任务 1:洗水壶 -> 烧开水 |
| 18 | + */ |
| 19 | + private CompletableFuture<Void> f1 = CompletableFuture.runAsync(()->{ |
| 20 | + System.out.println("T1: 洗水壶..."); |
| 21 | + ThreadUtil.sleep(1, TimeUnit.SECONDS); |
| 22 | + System.out.println("T1: 烧开水..."); |
| 23 | + ThreadUtil.sleep(15, TimeUnit.SECONDS); |
| 24 | + }); |
| 25 | + /** |
| 26 | + * 任务 2:洗茶壶 -> 洗茶杯 -> 拿茶叶 |
| 27 | + */ |
| 28 | + private CompletableFuture<String> f2 = CompletableFuture.supplyAsync(()->{ |
| 29 | + System.out.println("T2: 洗茶壶..."); |
| 30 | + ThreadUtil.sleep(1, TimeUnit.SECONDS); |
| 31 | + |
| 32 | + System.out.println("T2: 洗茶杯..."); |
| 33 | + ThreadUtil.sleep(2, TimeUnit.SECONDS); |
| 34 | + |
| 35 | + System.out.println("T2: 拿茶叶..."); |
| 36 | + ThreadUtil.sleep(1, TimeUnit.SECONDS); |
| 37 | + return " 龙井 "; |
| 38 | + }); |
| 39 | + /** |
| 40 | + * 任务 3:任务 1 和任务 2 完成后执行:泡茶 |
| 41 | + */ |
| 42 | + private CompletableFuture<String> f3 = f1.thenCombine(f2, (e, tf)->{ |
| 43 | + System.out.println("T1: 拿到茶叶:" + tf); |
| 44 | + System.out.println("T1: 泡茶..."); |
| 45 | + return " 上茶:" + tf; |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + public static void main(String[] args) { |
| 50 | + CompletableFutureExample example = new CompletableFutureExample(); |
| 51 | + System.out.println(example.f3.join()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 描述串行关系 thenApply |
| 56 | + */ |
| 57 | + static class SerialRelation{ |
| 58 | + private static CompletableFuture<String> f0 = |
| 59 | + CompletableFuture.supplyAsync( |
| 60 | + () -> "Hello World") //① |
| 61 | + .thenApply(s -> s + " QQ") //② |
| 62 | + .thenApply(String::toUpperCase);//③ |
| 63 | + |
| 64 | + public static void main(String[] args) { |
| 65 | + System.out.println(SerialRelation.f0.join()); |
| 66 | + } |
| 67 | + } |
| 68 | + /** |
| 69 | + * 描述汇聚Or关系 thenApply |
| 70 | + */ |
| 71 | + static class ConvergeRelation{ |
| 72 | + static CompletableFuture<String> f1 = |
| 73 | + CompletableFuture.supplyAsync(()->{ |
| 74 | + int t = getRandom(5, 10); |
| 75 | + ThreadUtil.sleep(t, TimeUnit.SECONDS); |
| 76 | + return String.valueOf(t); |
| 77 | + }); |
| 78 | + |
| 79 | + static CompletableFuture<String> f2 = |
| 80 | + CompletableFuture.supplyAsync(()->{ |
| 81 | + int t = getRandom(5, 10); |
| 82 | + ThreadUtil.sleep(t, TimeUnit.SECONDS); |
| 83 | + return String.valueOf(t); |
| 84 | + }); |
| 85 | + |
| 86 | + static CompletableFuture<String> f3 = |
| 87 | + f1.applyToEither(f2,s -> s); |
| 88 | + |
| 89 | + |
| 90 | + private static int getRandom(int i,int j){ |
| 91 | + return (int) (Math.random() * (j - i)) +i; |
| 92 | + } |
| 93 | + public static void main(String[] args) { |
| 94 | + |
| 95 | + System.out.println(ConvergeRelation.f3.join()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 处理异常 此例为发生异常默认为0 |
| 101 | + */ |
| 102 | + static class ExceptionHandler{ |
| 103 | + private static CompletableFuture<Integer> |
| 104 | + f0 = CompletableFuture |
| 105 | + .supplyAsync(()->7/0) |
| 106 | + .thenApply(r->r*10) |
| 107 | + .exceptionally(e->0); |
| 108 | + |
| 109 | + public static void main(String[] args) { |
| 110 | + System.out.println(ExceptionHandler.f0.join()); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
0 commit comments