Created
December 23, 2018 03:17
-
-
Save jcohen66/347a9759c80ef62a916b60961e755acc to your computer and use it in GitHub Desktop.
Completable Future #future #concurrency
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
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
public class Application { | |
public static void main(String[] args) throws ExecutionException, InterruptedException { | |
Future<String> completableFuture = calculateAsync(); | |
String result = completableFuture.get(); | |
System.out.println(result); | |
System.out.println(result instanceof String); | |
} | |
public static Future<String> calculateAsync() throws IncompatibleClassChangeError { | |
CompletableFuture<String> completableFuture = new CompletableFuture<>(); | |
Executors.newCachedThreadPool().submit(() -> { | |
Thread.sleep(500); | |
completableFuture.complete("Hello"); | |
return null; | |
}); | |
return completableFuture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment