The difference between thenapply () and Thencompose ():
Thenapply () is a non-completablefuture type returned:
its function is equivalent to converting completablefuture<t> into completablefuture<u>.
Thencompose () is used to connect two completablefuture, and the return value is the new completablefuture:
Summary: thenapply () conversion is the type of the generic, is the same completablefuture;
Thencompose () is used to connect two completablefuture and is to generate a new completablefuture.
Example:
1.thenApply ():
completablefuture<integer> future = Completablefuture.supplyasync ((), {
return 100;
});
completablefuture<string> f = future.thenapplyasync (i-i). thenapply (i-i.tostring ());
System.out.println (F.get ()); "1000"
Thenapply () Source code:
Public <U> completablefuture<u> Thenapply (
function<? Super T,? Extends u> fn) {
return uniapplystage (NULL, FN);
}
Interpretation:
Parameters: function<? Super T,? Extends u> fn)
Enter the value of T in the preceding completablefuture<t>, return the value of the type that inherits U, return this value
Order: First determine the return value U or its subclass, and then determine the <U> type in the method in public <U> completablefuture<u> thenapply ()
2.thenCompose ():
completablefuture<integer> future = Completablefuture.supplyasync ((), {
return 100;
});
completablefuture<string> f = future.thencompose (I, {
Return Completablefuture.supplyasync ((), {
Return (I * 10) + "";
});
});
System.out.println (F.get ()); 1000
The difference between thenapply () and Thencompose ()