3
    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

javaworld
  • 427
  • 1
  • 9
  • 19
  • The issue is you don't do anything with the `feature` `CompletableFuture` (a.k.a Promise) once it's instantiated. What do you want to do? do you want to wait until the future chain is completed? – Laksitha Ranasingha Mar 20 '19 at 18:11
  • 1
    i want the composeMethod to be executed – javaworld Mar 20 '19 at 18:13
  • This behavior can be changed: https://stackoverflow.com/questions/67164198/why-does-my-completablefuture-code-run-in-java-8-but-not-in-java-11 – user202729 Apr 22 '22 at 03:03

1 Answers1

7

Your task provided in the supplyAsync will execute in the ForkJoinPool#commonPool. And if you take a look on the thread in the common pool you can see that they are Deamon thread, that means JVM will not wait to Shutdown for that daemon thread to complete its execution when there is no active non-daemon thread.In your case, you have a sleep in composeMethod and in the meantime, main thread executes and completed its work, and JVM does not have any active non-daemon thread.So, JVM will go to shut down without waiting for your task to complete. If you run the below example you can confirm about the thread pool and type of thread.

CompletableFuture feature = CompletableFuture.supplyAsync(() -> {
    System.out.println("Thread : " + Thread.currentThread());
    System.out.println("Is Daemon : " + Thread.currentThread().isDaemon());
    return composeMethod();
}).thenAccept(s -> System.out.println("wassup java" + s)).thenRun(() -> System.out.println(" imm immortal"));

Output:

Thread : Thread[ForkJoinPool.commonPool-worker-1,5,main] // This line may be bit different but it indicates that your task is executing on Common pool
Is Daemon : true
why  should i wait for you 

To resolve the issue you can pass your own executor like below:

ExecutorService executorService = Executors.newFixedThreadPool(8);
CompletableFuture feature = CompletableFuture.supplyAsync(() -> {
        System.out.println("Thread : " + Thread.currentThread());
        System.out.println("Is Daemon : " + Thread.currentThread().isDaemon());
        return composeMethod();
    }, executorService).thenAccept(s -> System.out.println("wassup java" + s))
            .thenRun(() -> System.out.println(" imm immortal"));
executorService.shutdown();
nonblockingmethod();

Output:

Thread : Thread[pool-1-thread-1,5,main]
Is Daemon : false
why  should i wait for you 
wassup java   resultdaa
 imm immortal
Amit Bera
  • 7,075
  • 1
  • 19
  • 42