Grokking Java Interview Questions
0% completed
Future and CompletableFuture
CompletableFuture<Integer> total = CompletableFuture .supplyAsync(() -> priceOf("cherry")) // a slow call .thenApply(price -> price * 8); System.out.println("Order accepted"); System.out.println(total.join());
The variable total has a value before the price is known, because it holds a future: an object that represents a result which may arrive later. The program can print Order accepted while another thread still waits for the price, and only join() waits for the number.
The answers below start with the older Future interface
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%