I am very new to Spring and I am trying to call two methods from two separate classes a number of times, and I want each invocation to spin a new thread so they run concurrently. This is the code I have:
the main class:
@SpringBootApplication @EnableOAuth2Client @EnableAsync public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } } class SomeOtherClass { for (int i = 0; i < 100; i++) { Class1 class1 = new Class1(); class1.method1(//its arguments); } // doing other things // ... // method 2 will use the side effects of method 1, so ideally this next // for loop should start only after the previous one is over for (int i = 0; i < 50; i++) { Class2 class2 = new Class2(); class2.method2(//its arguments); } } public Class1 { @Async("threadPoolTaskExecutor") public void method1() throws Exception { LOGGER.info("Running this in {}", Thread.currentThread().getName()); } } public Class2 { @Async("threadPoolTaskExecutor") public void method2() throws Exception { LOGGER.info("Running this in {}", Thread.currentThread().getName()); } } @Configuration @EnableAsync public class ThreadConfig { @Bean("threadPoolTaskExecutor") public TaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(100); executor.setMaxPoolSize(100); executor.initialize(); return executor; } } The problem is when I run the application I only see one thread name printing, which means it's not running in a multithreaded way. Looking at the logs, I can also see the calls are being made sequentially. Having done multithreading using standard Java (Runnable), I know the multithreaded version should finish much faster. Since I am very new to Spring, I do not understand what I am doing wrong.
I have redacted the method names and logic, but the annotations and class structures are exactly thee same, so if you see any problem with that please point that out.