Closed benayat closed 7 months ago
Why are you using two different scheduler instances with the same configuration there? Each of those is going to have an independent scheduler thread, is that the goal? Otherwise, I rather recommend a unified scheduler with a single scheduler thread.
Technically, for scheduling purposes, SimpleAsyncTaskScheduler
in a virtual threads setup just uses a standard java.util.concurrent.ScheduledThreadPoolExecutor
with a single thread created via a virtual thread factory. Any kind of timing deviation is outside of our control there, this rather has to be an effect within the JDK ScheduledThreadPoolExecutor itself.
My intention is running 30 synchroneous tasks triggered by a Rest request from the client. Each task should take 2 minutes(running one after the other). Inside each task, I want to run 20 concurrent jobs, repeatedly every 10 secods - until the 2 minutes are up. Since @Scheduled annotation wasn't flexible enough for my use case, I used the Executors.newSingleThreadExecutor bean, running execute method for all synchroneous tasks, and similarly, a ThreadPoolTaskExecutor for the concurrentjobs. Can I achieve all that depending solely on spring schedullers, without executorService? or even better, a spring scheduler with virtual threads? Or is that too much of a stretch?
I am afraid there isn't really something actionable in terms of a bug report in the Spring Framework. As Juergen pointed out, the deviation you observe can certainly be reproduced without involving Spring at all. As for the code you've shared, having two SimpleAsyncTaskScheduler
is useless as they don't have any sort of pooling (the 30 vs. 20 you've described). For any follow-up questions on how to structure your configuration, please refer to StackOverflow.
I'm using SimpleAsyncTaskScheduler(tried both scheduleAtFixedRate, and scheduleWithFixedDelay), and 15-20 runs, the delay rate degrades from 3 seconds to sub-1 seconds.
spring boot version: 3.2.3. jdk version: 21.0.2.
my code to reproduce the problem:
spring.threads.virtual.enabled=true