Virtual Threads does not Stop after Application Shutdown

18 hours ago 1
ARTICLE AD BOX

I use:

@Configuration public class AsyncConfig { @Bean(name = "virtualExecutor") public ExecutorService newExecutor() { return Executors.newThreadPerTaskExecutor( Thread.ofVirtual() .name("virtual-", 0) .factory() ); } }

To create virtual threads and to run:

@Autowired @Qualifier("virtualExecutor") private ExecutorService virtualExecutor; public void myFunc(){ virtualExecutor.execute(() -> { try { f1(); } catch (Exception e) { log.error("Failed", e); } }); }

And:

private f1(){ for(...){ if (Thread.currentThread().isInterrupted()) { return false; } // my code } }

The threads are still running after application shutdown.
Using the below code to handle termination:

@Component public class ExecutorShutdownHandler { private final ExecutorService executor; public ExecutorShutdownHandler(@Qualifier("virtualExecutor") ExecutorService executor) { this.executor = executor; } @PreDestroy public void shutdown() { executor.shutdownNow(); }

Is there a solution to this? My code runs api calls using python Scripts via new ProcessBuilder()

Read Entire Article