Skip to content

Instantly share code, notes, and snippets.

@silentsudo
Created June 19, 2021 03:45
Show Gist options
  • Save silentsudo/64e2afc38538797c1d32c8f343004eda to your computer and use it in GitHub Desktop.
Save silentsudo/64e2afc38538797c1d32c8f343004eda to your computer and use it in GitHub Desktop.
example showing simple use of completable futures
package examples;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class SimpleCompletableFutureSequence {
static class CalculatedTimedResponse {
int difference;
String response;
public CalculatedTimedResponse(int difference, String response) {
this.difference = difference;
this.response = response;
}
@Override
public String toString() {
return "CalculatedTimedResponse{" +
"difference=" + difference +
", response='" + response + '\'' +
'}';
}
}
static class MyClass {
public String execute(String arg) {
return arg;
}
public CalculatedTimedResponse executeAnother(String arg) {
LocalTime startTime = LocalTime.now();
LocalTime endTime = LocalTime.now();
MyClass myClass = new MyClass();
String response = null;
try {
response = myClass.execute(arg);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
endTime = LocalTime.now();
}
Duration timeDifference = Duration.between(startTime, endTime);
System.out.println("Difference: " + timeDifference.toSecondsPart());
return new CalculatedTimedResponse(timeDifference.toSecondsPart(), response);
}
}
static CompletableFuture<String> class1() {
return CompletableFuture.completedFuture(new MyClass().execute("class1"));
}
static CompletableFuture<String> class2() {
return CompletableFuture.completedFuture(new MyClass().execute("class2"));
}
static CompletableFuture<String> class3() {
return CompletableFuture.completedFuture(new MyClass().execute("class3"));
}
static CompletableFuture<String> class4() {
return CompletableFuture.completedFuture(new MyClass().execute("class4"));
}
static CompletableFuture<CalculatedTimedResponse> class1Response = CompletableFuture.supplyAsync(new Supplier<CalculatedTimedResponse>() {
@Override
public CalculatedTimedResponse get() {
return new MyClass().executeAnother("time-execute- 1");
}
});
static CompletableFuture<CalculatedTimedResponse> class2Response = CompletableFuture.supplyAsync(new Supplier<CalculatedTimedResponse>() {
@Override
public CalculatedTimedResponse get() {
return new MyClass().executeAnother("time-execute- 2");
}
});
public static void main(String[] args) {
// List<CompletableFuture<String>> allFutures = Arrays.asList(class1(), class2(), class3(), class4());
// List<String> allResponse = allFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
// System.out.println(allResponse);
List<CompletableFuture<CalculatedTimedResponse>> timedFutures = Arrays.asList(class1Response, class2Response);
List<CalculatedTimedResponse> allTimeResponse = timedFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
System.out.println(allTimeResponse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment