I am using Spring on Java 21 and I am using ScheduledExecutorService to schedule some events, on executing the below code without try-with-resources it runs fine
Working Code:
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(() -> myMethod(), 0, 1, TimeUnit.MINUTES);
On trying to use it with try-with-resources as bellow, the executor do not run, I am not sure what is wrong here.
Not Working Code:
try (ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor()) {
executorService.scheduleAtFixedRate(() -> myMethod(), 0, 1, TimeUnit.MINUTES);
}
It would be great if someone could point what I am doing wrong.