0

Let's assume, doWork method runs so long and we set timeOut. In this case, does orTimeout kills the running task and the Thread?

    CompletableFuture<String> future = 
            doWork("JavaSampleApproach")
            .orTimeout(TIMEOUT, TimeUnit.SECONDS)
            .whenComplete((result, error) -> {
                if (error == null) {
                    System.out.println("The result is: " + result);
                } else {
                    System.out.println("Sorry, timeout in " + TIMEOUT + " seconds");
                }
            });
Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70
  • 2
    I'd _assume_ not as even `CompletableFuture.cancel(true)` won't interrupt the running task. At the very least, it definitely won't _kill_ the thread as that isn't how Java works. – Slaw Dec 15 '18 at 11:07
  • @Slaw you got it right, it’s similar to `cancel`, in other words, doesn’t affect a running task, simply because a `CompletableFuture` doesn’t even know running tasks. – Holger Jan 10 '19 at 16:06

0 Answers0