CompletableFuture improve your concurrent programming capability

Question: If there are two sequential execution time-consuming method, how would you do? ? ?

E.g:

public  void doHousework () { 
// boiling water doWater ();
// sweeping doFloor (); }

Yes, smart as I think you must have the use of two threads can easily solve this problem. So you and I do:

new Thread(()->doWater()) .start(); 

new Thread(()->doFloor()) .start();

The solution to this problem is to improve the efficiency

See here, I believe you should be able to understand why asynchronous programming in recent years the fire, because the performance optimization is a core demand of Internet giant ah. Java provides tools CompletableFuture to support asynchronous programming, CompletableFuture is a responsible version 1.8

How to understand CompletionStage Interface

I think you can stand in the perspective of the division of analogy about the workflow. Task is timing relationship, such as a serial relationship, parallel relations, pooling relations. Say this may be a bit abstract, but also give an example here in front of the tea kettle, kettle and boil water to wash which is a serial relationship, wash the kettle, boil water and wash the teapot, wash the cup is a parallel relationship between these two sets of tasks, boil water, tea and tea is to take off CompletionStage aggregation interface may clearly describe the timing relationship between the tasks, such as the aforementioned f3 = f1.thenCombine (f2, () -> {}) is described in a kind of convergence relations. Convergence program in relation to boil water to make tea is an aggregation relationship AND, AND here means that after all dependent tasks (boil water and take tea) have been completed to start execution of the current task (tea). Since there AND aggregation relationship, then there must be aggregation relationship OR, OR refers to the so-called dependent task as long as one can execute the current task is completed. In the field of programming, there is a hill around the past, that is, exception handling, CompletionStage interface can also be conveniently described exception handling. Here we are introduced one by one, how CompletionStage serial interface descriptions relations, AND aggregation relationship

 

1. Description Serial relations

CompletionStage serial interfaces which describe relations, mainly thenApply, thenAccept, thenRun and thenCompose four series of interfaces. thenApply parameter family of functions fn in the type of the interface Function <T, R>, methods on this interface is associated with CompletionStage R apply (T t), this method can also receive parameters supported return value, the method returns to the series thenApply is CompletionStage <R>. The type thenAccept series method in parameter consumer is the interface Consumer <T>, the method in this interface is associated with CompletionStage void accept (T t), although this method supports the parameters, but does not support return values, the method returns thenAccept series It is CompletionStage <Void>.

 thenRun series in action method parameters are Runnable, so action can neither receive parameters do not support return values, so thenRun series method returns also CompletionStage <Void>. These methods are representative of which are asynchronous Async fn, consumer or action. Among them, you need to note that thenCompose series method, this series will be a new method to create a child process, the end result is the same and thenApply series.

CompletionStage<R> thenApply(fn);
CompletionStage<R> thenApplyAsync(fn);
CompletionStage<Void> thenAccept(consumer);
CompletionStage<Void> thenAcceptAsync(consumer);
CompletionStage<Void> thenRun(action);
CompletionStage<Void> thenRunAsync(action);
CompletionStage<R> thenCompose(fn);
CompletionStage<R> thenComposeAsync(fn);

 

By the following sample code, you can look at thenApply () method is how to use. First start an asynchronous process by supplyAsync (), followed by two serial operation, the overall look was quite simple. However, while this is an asynchronous process, but the task ①②③ is serial execution, ① ② dependence of the results, ③ ② dependence of the results.

CompletableFuture<String> f0 = 
  CompletableFuture.supplyAsync(
    () -> "Hello World")      //
  .thenApply(s -> s + " QQ")  //
  .thenApply(String::toUpperCase);//

System.out.println(f0.join());
// 输出结果
HELLO WORLD QQ

2. DESCRIPTION AND aggregation relationship

CompletionStage interfaces which describe AND convergence relations, mainly thenCombine, thenAcceptBoth and runAfterBoth series of interfaces, these interfaces are also distinguished from fn, consumer, action three core parameters are different. They use you can refer to the above procedures for boil water to make tea, not go into here.

CompletionStage<R> thenCombine(other, fn);
CompletionStage<R> thenCombineAsync(other, fn);
CompletionStage<Void> thenAcceptBoth(other, consumer);
CompletionStage<Void> thenAcceptBothAsync(other, consumer);
CompletionStage<Void> runAfterBoth(other, action);
CompletionStage<Void> runAfterBothAsync(other, action);

 

Guess you like

Origin www.cnblogs.com/clovejava/p/11305870.html