1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import java.util.concurrent.*; import java.util.*; import java.util.stream.*; public class TestPromise{ public static void main(String[] argvs) throws Exception{ List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); list.forEach(n -> System.out.println(n)); System.out.println("-------"); list.forEach(System.out::println); System.out.println("-------"); String s = CompletableFuture.supplyAsync(() -> { System.out.println("enter s1"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s1"); return "hello"; }).thenCombine(CompletableFuture.supplyAsync(() -> { System.out.println("enter s2"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s2"); return "world"; }), (s1, s2) -> {return s1 + " " + s2;} ).join(); System.out.println(s); System.out.println("-------"); Map<String, String> result = new HashMap<String, String>(); CompletableFuture.allOf( CompletableFuture.supplyAsync(() -> { System.out.println("enter s1"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s1"); result.put("k1", "11111"); return "11"; }), CompletableFuture.supplyAsync(() -> { System.out.println("enter s2"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s2"); result.put("k2", "22222"); return "22"; }), CompletableFuture.supplyAsync(() -> { System.out.println("enter s3"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s3"); result.put("k3", "33333"); return "33"; }) ).get(); System.out.println(result.get("k1") + " " + result.get("k2") + " " + result.get("k3")); System.out.println("\r\n-------------------\r\n"); CompletableFuture<Map<String, Object>> cf1 = CompletableFuture.supplyAsync(() -> { System.out.println("enter s1"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s1"); Map<String, Object> map = new HashMap<String, Object>(); map.put("s1", "11111"); return map; }); CompletableFuture<Map<String, Object>> cf2 = CompletableFuture.supplyAsync(() -> { System.out.println("enter s2"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("s2"); Map<String, Object> map = new HashMap<String, Object>(); map.put("s2", "22222"); return map; }); CompletableFuture<Map<String, Object>> cf3 = CompletableFuture.supplyAsync(() -> { System.out.println("enter s3"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } Map<String, Object> map = new HashMap<String, Object>(); map.put("s3", "33333"); return map; }); Map<String, Object> resultMap = new HashMap<String, Object>(); Stream.of(cf1, cf2, cf3).map(CompletableFuture::join).forEach(v -> resultMap.putAll(v)); System.out.println(resultMap); } }
|