Last active
April 4, 2018 06:14
-
-
Save adamw/a8566cfaa6b38234a667a7893219347b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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