I have a scheduled task which needs to launch multiple threads of the same process when executed, is it possible to set a specific number of threads to be launched when the process is kicked off?
In the application class I have the following TaskExecutor beans configured
@Bean("threadFooExecutor")
public TaskExecutor getFooExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("Foo-");
return executor;
}```
@Bean("threadBarExecutor")
public TaskExecutor getBarExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("Bar-");
return executor;
}
Async processes configured in process class
@Async("threadFooExecutor")
@Scheduled(fixedRate = 3000, initialDelay = 5000)
public void print() {
System.out.println(Thread.currentThread().getName() + " " + 1);
System.out.println(Thread.currentThread().getName() + " " + 2);
System.out.println(Thread.currentThread().getName() + " " + 3);
System.out.println(Thread.currentThread().getName() + " " + 4);
}
@Async("threadBarExecutor")
@Scheduled(fixedRate = 3000, initialDelay = 5000)
public void print2() {
System.out.println(Thread.currentThread().getName() + " " + 1);
System.out.println(Thread.currentThread().getName() + " " + 2);
System.out.println(Thread.currentThread().getName() + " " + 3);
System.out.println(Thread.currentThread().getName() + " " + 4);
}
What I would like to see is 2 or 3 of each of these threads running at the same time, but I only see each thread being run once every 3 seconds