Reputation: 353
I am using Spring ThreadPoolTaskScheduler to execute a task and I am scheduling the same task inside the run()
method as below. By doing so I am making sure the next is scheduled after the current task is completed.
public class Task implements Runnable{
@Autowired
ThreadPoolTaskScheduler scheduler;
public void run() {
//some work
scheduler.schedule(this, new Date(System.currentTimeMillis() + delay));
}
}
Is this approach safe, will there be any concurrency issues?
Upvotes: 0
Views: 703
Reputation: 73578
Instead of trying to hand configure the next execution, you should use ThreadPoolTaskScheduler.scheduleWithFixedDelay(...)
. It does exactly what your code does, except better.
Upvotes: 2