2016-12-21 11:06:49 -08:00
|
|
|
// concurrent/CompletableApplyAsync.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2016-12-21 11:06:49 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://OnJava8.com for more book information.
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import onjava.*;
|
|
|
|
|
|
|
|
public class CompletableApplyAsync {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Timer timer = new Timer();
|
|
|
|
CompletableFuture<Machina> cf =
|
|
|
|
CompletableFuture.completedFuture(
|
|
|
|
new Machina(0))
|
|
|
|
.thenApplyAsync(Machina::work)
|
|
|
|
.thenApplyAsync(Machina::work)
|
|
|
|
.thenApplyAsync(Machina::work)
|
|
|
|
.thenApplyAsync(Machina::work);
|
|
|
|
System.out.println(timer.duration());
|
|
|
|
System.out.println(cf.join());
|
|
|
|
System.out.println(timer.duration());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2021-01-31 15:42:31 -07:00
|
|
|
103
|
2016-12-21 11:06:49 -08:00
|
|
|
Machina0: ONE
|
|
|
|
Machina0: TWO
|
|
|
|
Machina0: THREE
|
|
|
|
Machina0: complete
|
|
|
|
Machina0: complete
|
2021-01-31 15:42:31 -07:00
|
|
|
545
|
2016-12-21 11:06:49 -08:00
|
|
|
*/
|