Skip to content

Instantly share code, notes, and snippets.

@Yatharth0045
Last active April 7, 2019 16:12
Show Gist options
  • Save Yatharth0045/b5273bfd8190ebeaa3f9c6ebc077ecd7 to your computer and use it in GitHub Desktop.
Save Yatharth0045/b5273bfd8190ebeaa3f9c6ebc077ecd7 to your computer and use it in GitHub Desktop.
This program is to show the manual completion of completablefuture.
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