Skip to content

Instantly share code, notes, and snippets.

@Danushka96
Created August 20, 2019 17:42
Show Gist options
  • Save Danushka96/4af17bcf4c0f9ad735a154586ebf33de to your computer and use it in GitHub Desktop.
Save Danushka96/4af17bcf4c0f9ad735a154586ebf33de to your computer and use it in GitHub Desktop.
CompletableFuture Java Implementation
package com.ustack.reactor.examples;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Hello {
public static void main(String args[]) throws InterruptedException, ExecutionException {
//With completable future example
Hello hello = new Hello();
Future<String> completableFuture = hello.calculateAsync();
String result = completableFuture.get();
System.out.println(result);
}
//Completable future implementation
private Future<String> calculateAsync() {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(()->{
Thread.sleep(1000);
System.out.println("done wait");
completableFuture.complete("hello");
return null;
});
System.out.println("second");
return completableFuture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment