Not introduced, directly attached to the code, it is recommended to use in your code, it is really easy
cn.hou.completablefuture Package Penalty for; Import org.junit.Test; . Import java.util.concurrent * ; public class CompletableFutureDemo { / * * * In Java8, CompletableFuture provides a very powerful extensions Future can help us simplify complex asynchronous programming, * and provides the ability to functional programming, may be processed results by the callback mode, but also provides a conversion and method of combining CompletableFuture of * * Note: the method has Async generally represent other from a thread, no is represented by the current thread * / @Test public void Test01 () throws Exception { ExecutorService-Service = Executors.newFixedThreadPool ( . 5 ); / * * * supplyAsync for the task returns a value, * runAsync task is no return value for * Executor parameters may manually specify the thread pool, otherwise the default ForkJoinPool.commonPool () system-level common thread pool * / CompletableFuture <String> Future CompletableFuture.supplyAsync = (() -> { the try { TimeUnit.SECONDS.sleep ( . 3 ); } the catch (InterruptedException E) { e.printStackTrace (); } return " Hou sign " ; },-Service); CompletableFuture . <Void> Data = CompletableFuture.runAsync (() -> the System OUT .println ( " Hou sign " )) ; / * * * The results completion callback * / future.whenComplete ((the X-, the y-) -> System. OUT .println (the X-+ " , " + the y-)); // thread execution of the current task execution continues data.whenCompleteAsync ((x, the y-) -> System. OUT .println (the X-+ " , " + the y-)); // to start a new thread pool thread executes future.exceptionally (Throwable :: toString); // System.out.println (Future.get ()); / * * * thenApply, a thread can rely on another thread, abnormal is not performed * / // second thread dependences first result CompletableFuture <Integer> future1 = CompletableFuture.supplyAsync ( () - > 5) .thenApply (X -> X); / * * * tasks handle is finished processing the results, the first abnormal continue * / CompletableFuture <Integer> = future2 future1.handleAsync ((X, Y) - > X + 2 ); the System. OUT .println (future2. GET ()); // . 7 / * * * thenAccept consumption processing result, does not return * / future2.thenAccept (the System. OUT :: the println); / * * * thenRun not care about the result of the processing tasks. As long as the above task execution is completed, they begin to execute * / future2.thenRunAsync (() -.> System OUT .println ( " proceed to the next task" )); / * * * ThenCombine will perform two tasks CompletionStage After completion, the results of two tasks to be handled thenCombine * / CompletableFuture <Integer> = future3 future1.thenCombine (future2, Integer :: SUM) ; the System. OUT .println (future3. GET ()); // . 5. 7 = + 12 is / * * * thenAcceptBoth: when two CompletionStage are executed, the result of a thenAcceptBoth to be consumed * / future1.thenAcceptBothAsync (future2, (X, Y) -> the System. OUT .println (X + " , " + Y)); // 5,7 / * * ApplyToEither * * two CompletionStage, who performed the fast return of the results, the next step I with the results of that conversion action of CompletionStage * / CompletableFuture <Integer> = future4 future1.applyToEither (future2, X -> X); . The System OUT .println (future4. GET ()); // 5 / * * * acceptEither * two CompletionStage, who performed the fast return of the result, I would be the next step with the result that CompletionStage consuming operation * / future1.acceptEither (future2 ., the System OUT :: the println); / * * * runAfterEither * two CompletionStage, either complete the next operation will be executed (the Runnable * / future1.runAfterEither (Future, () . -> System OUT .println ( " there is a complete, I continue " )); / * * * runAfterBoth * two CompletionStage, have completed the calculation will be performed next operation ( Runnable) * / future1.runAfterBoth (Future, () . -> System OUT .println ( " have been completed, I continue " )); / * * * thenCompose method * thenCompose method allows you to more CompletionStage pipelining, upon completion, the first operation result is transmitted as a parameter to a second operation * thenApply accepts a function, thenCompose accept a future instance, is more suitable for the operation processing flow * / future1.thenComposeAsync (X-> CompletableFuture.supplyAsync (() -> X + . 1 )) .thenComposeAsync (X -> CompletableFuture.supplyAsync (() -> X + 2 )) .thenCompose (X -> CompletableFuture.runAsync (() -.> The System OUT . the println ( " flow result: " + X))); TimeUnit.SECONDS.sleep ( . 5 ); // main thread sleep, waiting for other threads executed } }