Last active
April 7, 2019 16:12
-
-
Save Yatharth0045/b5273bfd8190ebeaa3f9c6ebc077ecd7 to your computer and use it in GitHub Desktop.
This program is to show the manual completion of completablefuture.
This file contains hidden or 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.TimeUnit; | |
class Test { | |
public static void main(String[] args) throws ExecutionException, InterruptedException { | |
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { | |
try { | |
TimeUnit.SECONDS.sleep(1000); | |
} catch (InterruptedException ex) { | |
ex.printStackTrace(); | |
} | |
return "Completed automatically"; | |
}); | |
if(completableFuture.isDone()){ | |
completableFuture.get(); | |
}else{ | |
completableFuture.complete("Completed manually"); | |
} | |
System.out.println(completableFuture.get()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment