I am trying to understand the behavior of @Async in Spring Boot, by using the default SimpleAsyncTaskExecutor (where I don't explicitly define any Executor bean). According to the documentation of SimpleAsyncTaskExecutor, 'By default, the number of concurrent threads is unlimited'. But on running the sample code below, all I can see is that only 8 threads are fired up, and the remaining tasks are waiting to get a new thread to execute them. I know this can be prevented with a custom Executor where I can define the size of the thread pool. But I want to know if my understanding of SimpleAsyncTaskExecutor is correct or not, or something is not right with my code.
Main class
@SpringBootApplication
@EnableAsync
public class MainRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(MainRunner.class);
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(MainRunner.class);
MyService myService = (MyService) applicationContext.getBean("myService");
LOGGER.info("Starting the submission of tasks...");
for (int i = 1; i <= 50; i++) {
myService.doSomething("Number" + i);
}
LOGGER.info("Finished submission of tasks...");
}
}
MyService class
@Service
public class MyService {
private static final Logger LOGGER = LoggerFactory.getLogger(MyService.class);
@Async
public void doSomething(String userName) {
LOGGER.info(Thread.currentThread().getName() + ", "
+ Thread.currentThread().getId() + ", NAME: " + userName + " STARTING...");
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 1000000; j++) {
int res = i + j;
}
}
LOGGER.info(Thread.currentThread().getName() + ", "
+ Thread.currentThread().getId() + ", NAME: " + userName + " COMPLETE...");
}
}
I expect to see that all 50 tasks are started, and they don't wait for a thread ready to process them. But the above code causes first 8 tasks submitted to start off, and the remaining tasks are waiting for the running tasks to complete in order to be picked up and executed.
2019-09-19 09:33:06.560 INFO 17376 --- [ main] sample.MainRunner : Starting the submission of tasks...
2019-09-19 09:33:06.564 INFO 17376 --- [ main] sample.MainRunner : Finished submission of tasks...
2019-09-19 09:33:06.566 INFO 17376 --- [ task-8] sample.MyService : task-8, 45, NAME: Number8 STARTING...
2019-09-19 09:33:06.566 INFO 17376 --- [ task-1] sample.MyService : task-1, 38, NAME: Number1 STARTING...
2019-09-19 09:33:06.566 INFO 17376 --- [ task-7] sample.MyService : task-7, 44, NAME: Number7 STARTING...
2019-09-19 09:33:06.567 INFO 17376 --- [ task-4] sample.MyService : task-4, 41, NAME: Number4 STARTING...
2019-09-19 09:33:06.566 INFO 17376 --- [ task-6] sample.MyService : task-6, 43, NAME: Number6 STARTING...
2019-09-19 09:33:06.567 INFO 17376 --- [ task-2] sample.MyService : task-2, 39, NAME: Number2 STARTING...
2019-09-19 09:33:06.567 INFO 17376 --- [ task-5] sample.MyService : task-5, 42, NAME: Number5 STARTING...
2019-09-19 09:33:06.567 INFO 17376 --- [ task-3] sample.MyService : task-3, 40, NAME: Number3 STARTING...
It waits for the first 8 to complete, and then the remaining tasks are executed. Is my understanding of SimpleAsyncTaskExecutor wrong here?