CompletableFuture feature = CompletableFuture.supplyAsync (() -> composeMethod ( )).
thenAccept (s -> System.out.println ("wassup java" + s)).
thenRun (() -> System.out.println (" imm immortal"));
nonblockingmethod ( );
This is the CompletableFuture future example im working on
private static void nonblockingmethod() {
System.out.println ("why should i wait for you ");
}
private static String composeMethod() {
try {
TimeUnit.MILLISECONDS.sleep (3);
File file = Paths.get ("/Users/solo/solo1.txt").toFile ( );
if (!file.exists ( )) {
file.createNewFile ( );
}
} catch (Exception e) {
}
return " resultdaa";
}
first i call compose method from supplyAsync , where i execute the method composeMethod , there is a three millisecond delay and then it will create a file and return a string as result . after its completion i call thenRun method which just prints a method , After that there is a nonblockingmethod which runs from the main thread .
the problem i face here is the main Thread finish executing the nonblockingmethod() and exits the process before the 3 millisecond delay while the subsequent operation from composeMethod. is this teh expected behaviour or i have to block the main thread using get or join , or i miss anything