|
| 1 | +package com.java8.completefuture; |
| 2 | + |
| 3 | +import org.apache.log4j.LogManager; |
| 4 | +import org.apache.log4j.Logger; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.*; |
| 10 | + |
| 11 | +public class WhenCompleteDemo { |
| 12 | + |
| 13 | + |
| 14 | + private static Logger logger= LogManager.getLogger(WhenCompleteDemo.class); |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + whenComplete(); |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + public static void whenComplete() { |
| 22 | + //该线程池仅用于示例,实际建议使用自定义的线程池 |
| 23 | + ExecutorService executorService = Executors.newCachedThreadPool(); |
| 24 | + String word1 = "word1"; |
| 25 | + String word2 = "word2"; |
| 26 | + |
| 27 | + CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync( |
| 28 | + () -> getResult(word1, 1000), executorService) |
| 29 | + .whenComplete((result, throwable) ->{ |
| 30 | + if (throwable != null) { |
| 31 | + logger.error("completableFuture1 error:{}", throwable); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync( |
| 36 | + () -> getResult(word2, 1500), executorService) |
| 37 | + .whenComplete((result, throwable) ->{ |
| 38 | + if (throwable != null) { |
| 39 | + logger.error("completableFuture2 error:{}", throwable); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + List<CompletableFuture<String>> futureList = new ArrayList<>(); |
| 44 | + futureList.add(completableFuture1); |
| 45 | + futureList.add(completableFuture2); |
| 46 | + |
| 47 | + List<String> resultList = new ArrayList<>(); |
| 48 | + //allOf()聚合所有任务,join阻塞 |
| 49 | + CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join(); |
| 50 | + futureList.forEach(f -> resultList.add(getFutureResult(f,2000))); |
| 51 | + |
| 52 | + |
| 53 | + //以下这段代码,拿不到返回值,值得思考 |
| 54 | +// CompletableFuture<Void> allFutures = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])); |
| 55 | +// allFutures.thenRun(() -> |
| 56 | +// futureList.forEach(f -> resultList.add( getFutureResult(f)))); |
| 57 | + |
| 58 | + resultList.forEach(System.out::println); |
| 59 | + } |
| 60 | + |
| 61 | + private static String getResult(String result, int millis) { |
| 62 | + try { |
| 63 | + //任务耗时。可以分别设置1000和3000,看未超时和超时的不同结果。 |
| 64 | + Thread.sleep(millis); |
| 65 | + } catch (InterruptedException e) { |
| 66 | + logger.error("supplyAsyncGet error."); |
| 67 | + } |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + private static String getFutureResult(CompletableFuture<String> completableFuture, int timeout) { |
| 72 | + String result = null; |
| 73 | + try { |
| 74 | + //获取返回值,2秒超时 |
| 75 | + result = completableFuture.get(timeout, TimeUnit.SECONDS); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + logger.error("InterruptedException error.", e); |
| 78 | + } catch (ExecutionException e) { |
| 79 | + logger.error("ExecutionException error.", e); |
| 80 | + } catch (TimeoutException e) { |
| 81 | + logger.error("TimeoutException error.", e); |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments