CompletableFuture
is not working as expected when I am using I/O operation in supplyAsync
/thenApply
etc. methods
I am new to CompletableFuture
, so I guess there is something that I am missing.
private static void completableTest() {
CompletableFuture.supplyAsync(() -> {
GET();
writeToFile("sync");
System.out.println("sync");
return "";
}).thenApply(t -> {
GET();
writeToFile("apply");
System.out.println("apply");
return "";
}).thenAccept(t -> {
writeToFile("accept");
System.out.println("accept");
});
GET()
makes an httpGet call;
writeToFile()
writes the content to file;
My file is also empty when I use GET()
or replace it with Thread.sleep()
. However it works fine if I don't use GET()
.
NOTE : It works fine when I use join()
at the end. (File has the correct output).