Executors.newVirtualThreadPerTaskExecutor() leaks CompletableFuture

Alan Bateman Alan.Bateman at oracle.com
Wed Mar 15 07:55:37 UTC 2023


On 14/03/2023 20:41, Kasper Nielsen wrote:
> I don't know if it is much of an issue in practice but you can cast the
> futures returned from Executors.newVirtualThreadPerTaskExecutor
> to CompletableFuture:
>
> ExecutorService e = Executors.newVirtualThreadPerTaskExecutor();
> CompletableFuture f = (CompletableFuture<?>) e.submit(() -> {});
>
> Other executors return instances of FutureTask which is probably less
> likely to be abused.
Indeed, FutureTask allows for sufficient customization in this context 
and probably less tempting to cast to.

-Alan



More information about the loom-dev mailing list