title: CompletableFuture long running task with time out would it hold thread given composed with timeout future?
body: <p>In the following code intention is to achieve asynchronous execution of a task provided it runs in less than 100 ms else timeout exception from other thread would be thrown , </p>
  
  <p><strong>Question</strong>  : What happens to the thread that is executing asynchronously for more than 100 ms , would it be released ? or it would wait for the task to complete without throwing exception ? ( wrote the code thinking that acceptEither would terminate the long running task once it encounters the timeOutException from the other future) </p>
  
  <pre><code>private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
      public static &lt;T&gt; CompletableFuture&lt;T&gt; failAfter(long longValue) {
          final CompletableFuture&lt;T&gt; timeoutFuture = new CompletableFuture&lt;&gt;();
          scheduler.schedule(() -&gt; {
              final TimeoutException ex = new TimeoutException("Timing out");
              return timeoutFuture.completeExceptionally(ex);
          }, longValue, TimeUnit.MILLISECONDS);
          return timeoutFuture;
      }  
  
  final CompletableFuture&lt;CustomResponse&gt; CustomResponseFuture = CompletableFuture.supplyAsync(() -&gt; object.taks()).
                  exceptionally(ex -&gt; {
                      System.out.println("help");
                      return new "Hello";
                      });
  
          CustomResponseFuture
          .acceptEither(failAfter(100l), TesFutureStuff::myNewMethod)
          .exceptionally(throwable -&gt; {
              System.out.println("failure "+throwable.getMessage());
               return null;
          })/*.getNow(null )*/;
  </code></pre>