I have task:
@Scheduled(fixedDelayString = "3000")
public void test() {
System.out.println(Thread.currentThread().getName());
}
And I want to have 3 instances of this task, I specified ThreadPoolTaskExecutor which has 3 pools, but I get 3 tasks which wait others execution, how can I run them parallel without waiting other tasks?
CodePudding user response:
The only solution I can think of right now (without digging deeper into the documentation) is to have your task scheduled once and it starts three threads.
First, you create a ExecutorService executorService = Executors.newFixedThreadPool(3);
and have this as a static variable.
In your @Scheduled
method, you do the processing 3 times by calling executor.submit()
3 times with the same underlying method (you extract the actual processing part in a separate method).