Spring provides a convenient way of implement scheduler with the use of
ThreadPoolTaskScheduler.
The following passage is from
spring docs:
As a convenience, Spring also provides a ThreadPoolTaskScheduler
, which internally delegates to a ScheduledExecutorService
to provide common bean-style configuration along the lines of ThreadPoolTaskExecutor
. These variants work perfectly fine for locally embedded thread pool setups in lenient application server environments, as well — in particular on Tomcat and Jetty.
Sometimes tasks takes longer than usual to execute and each task that will run uses up a thread from the pool. If it will take some time for a task to complete then it will take some time for that thread to be returned to pool. This delay can cause other task to wait for a thread to be available from the pool because there are no more free threads in the pool.
It is unlikely that the example above would cause thread exhaustion as it is just printing the current date and time, however, most actual scheduled tasks are performing more complicated actions.