Reputation: 45
given the following method:
private static String getChuckNorrisJoke () {
try {
HttpURLConnection con = (HttpURLConnection) new
URL( "http://api.icndb.com/jokes/random" ).openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null ) {
response.append(line);
}
in.close();
return response.toString();
} catch (IOException e) {
throw new IllegalStateException( "Something is wrong: " , e);
}
}
the following statement can be used to run the method in an asynchronous fashion.
final CompletableFuture<String> jokeAsync = CompletableFuture.supplyAsync(() -> getChuckNorrisJoke());
although I think that I understand CompletionStage
relation to CompletableFuture
, I am not sure how I can use CompletionStage
to accomplish same task.
final CompletionStage<String> jokeAsync = ?
also, I am not sure about "combining stages"
Upvotes: 2
Views: 4966
Reputation: 20579
CompletionStage
is the interface implemented by CompletableFuture
, so you can just declare jokeAsync
as a CompletionStage
and it will work:
final CompletionStage<String> jokeAsync = CompletableFuture.supplyAsync(() -> getChuckNorrisJoke());
If you have several stages, you can combine them in different ways, like:
thenCombine()
to combine the result of 2 stagesapplyToEither()
to process the result of the first one that completes and return a new stage with the resultacceptEither()
to consume the result of either without returning a new resultrunAfterBoth()
and runAfterEither()
to run an operation after both or either stage is completed.The CompletionStage
API does not offer some operations that are only provided by CompletableFuture
:
supplyAsync()
allOf()
and anyOf()
join()
or get()
But the toCompletableFuture()
allows to convert any stage and thus bridge the gap.
Upvotes: 6