Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2  = CompletableFuture.supplyAsync(() -> "Beautiful");
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
combinedFuture.get();
assertTrue(future1.isDone());
assertTrue(future2.isDone());
assertTrue(future3.isDone());
// the result of each one can be processed as follows:
String combined = Stream.of(future1, future2, future3)
.map(CompletableFuture::join)
.collect(Collectors.joining(" "));
public class Main {
public class RunnableJob implements Runnable{
private final String str;
private final Consumer<JobResult> consumer;
public RunnableJob(Consumer<JobResult> consumer, String str){
this.str = str;
this.consumer = consumer;
}
@Override
public void run() {
int sec = (int) (Math.abs(Math.random())*6 + 1);
System.out.println("Running "+ str + " - takes " + sec + " seconds");
try {
Thread.sleep(sec*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
consumer.accept(new JobResult(str));
}
}
public void runJobs(){
Consumer<JobResult> consumer = (input)->{
System.out.println("Consuming " + input.toString());
};
List<CompletableFuture<Void>> futures = new ArrayList<>();
for(int i=0;i<10;i++) {
futures.add(CompletableFuture
.runAsync(new RunnableJob(consumer, "Job " + i))
.thenRun(new RunnableJob(consumer, "job " + i + " finished")));
}
futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.<Void>toList());
System.out.println("REACHED END OF runJobs");
}
public static void main(String[] args){
new Main().runJobs();
}
}
/****
Output is:
---------------------------------------
Running Job 0 - takes 1 seconds
Running Job 1 - takes 2 seconds
Running Job 3 - takes 2 seconds
Running Job 2 - takes 1 seconds
Running Job 4 - takes 6 seconds
Running Job 5 - takes 5 seconds
Running Job 6 - takes 5 seconds
Consuming JobResult:Job 0
Consuming JobResult:Job 2
Running job 2 finished - takes 2 seconds
Running job 0 finished - takes 4 seconds
Consuming JobResult:Job 3
Consuming JobResult:Job 1
Running job 1 finished - takes 5 seconds
Running job 3 finished - takes 4 seconds
Consuming JobResult:job 2 finished
Running Job 7 - takes 3 seconds
Consuming JobResult:Job 6
Consuming JobResult:job 0 finished
Consuming JobResult:Job 5
Running Job 8 - takes 6 seconds
Running job 6 finished - takes 3 seconds
Running job 5 finished - takes 6 seconds
Consuming JobResult:Job 4
Running job 4 finished - takes 5 seconds
Consuming JobResult:job 3 finished
Consuming JobResult:Job 7
Running job 7 finished - takes 1 seconds
Running Job 9 - takes 2 seconds
Consuming JobResult:job 1 finished
Consuming JobResult:job 7 finished
Consuming JobResult:job 6 finished
Consuming JobResult:Job 9
Running job 9 finished - takes 5 seconds
Consuming JobResult:job 4 finished
Consuming JobResult:job 5 finished
Consuming JobResult:Job 8
Running job 8 finished - takes 1 seconds
Consuming JobResult:job 8 finished
Consuming JobResult:job 9 finished
REACHED END OF runJobs
***/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment