Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active April 4, 2018 06:14
Show Gist options
  • Save adamw/a8566cfaa6b38234a667a7893219347b to your computer and use it in GitHub Desktop.
Save adamw/a8566cfaa6b38234a667a7893219347b to your computer and use it in GitHub Desktop.
package test;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
public class Compare2Wrappers {
// I/O operations: non-blocking, asynchronous
CompletableFuture<String> sendHttpGet(String url) { return null; }
// the business logic: asynchronous, as the type suggests
CompletableFuture<String> runBusinessProcess(long userId) throws InterruptedException {
// running two API calls; if threads are available, these will run in parallel
CompletableFuture<String> profileResult =
sendHttpGet("http://profile_service/get/" + userId);
CompletableFuture<String> friendsResult =
sendHttpGet("http://friends_service/get/" + userId);
// composing both futures into a single result
return profileResult.thenCompose(profile ->
friendsResult.thenApply(friends ->
"Profile: " + profile + ", friends: " + friends));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment