Created
August 20, 2019 17:42
-
-
Save Danushka96/4af17bcf4c0f9ad735a154586ebf33de to your computer and use it in GitHub Desktop.
CompletableFuture Java Implementation
This file contains 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 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