title: How to best implement a multi-thread approach in Spring Boot
body: <p>I have been struggling with implementing a multi-threaded approach to the application I am working on.</p>
  <p>The part I want to run in parallel threads was originally constructed with a for loop going about a list.</p>
  <pre class="lang-java prettyprint-override"><code>@Service
  public ApplicationServiceImpl implements ApplicationService {
  
      @Override
      public ResponseEntity&lt;Void&gt; startProcess(List&lt;MyObject&gt; myObjectList) throws Exception {
          for (MyObject myObject : myObjectList) {
              AnotherTypeOfObject anotherTypeOfObject = runMethodA(myObject);
              YetAnotherTypeOfObject yetAnotherTypeOfObject = runMethodB(anotherTypeOfObject);
              runMethodC(yetAnotherTypeOfObject, aStringValue, anotherStringValue);
              runMethodD(yetAnotherTypeOfObject);
          }
      }
  }
  </code></pre>
  <p>The methods <code>private AnotherTypeOfObject runMethodA(MyObject myObject) {...}</code>, <code>private YetAnotherTypeOfObject yetAnotherTypeOfObject(AnotherTypeOfObject anotherTypeOfObject) {...}</code>, <code>private void runMethodC(YetAnotherTypeOfObject yetAnotherTypeOfObject, String aStringValue, String anotherStringValue) {...}</code> and <code>private void runMethodD(MyObject myObject) {...}</code> only use local variables.</p>
  <p>I have looked quite a bit to get a solution that would allow firing the threads of a list of 100s of <code>MyObject</code> instead of one after the other.</p>
  <p>What I have done is create a:</p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableAsync
  public class AsyncConfiguration() {
  
      @Bean(name = &quot;threadPoolTaskExecutor&quot;)
      public Executor aSyncExecutor() {
          final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
          threadPoolTaskExecutor.setCorePoolSize(4);
          threadPoolTaskExecutor.setMaxPoolSize(4);
          threadPoolTaskExecutor.setQueueCapacity(50);
          threadPoolTaskExecutor.setThreadNamePrefix(&quot;threadNamePrefix&quot;);
          threadPoolTaskExecutor.initialize();
          return threadPoolTaskExecutor;
      }
  }
  </code></pre>
  <p>I do have loads of <code>log.info(&quot;some recognizable text&quot;)</code> through the methods A, B, C and D so I can make sure what is going on and I aggregated these methods into one like</p>
  <pre class="lang-java prettyprint-override"><code>private void runThreads(MyObject myObject, String aStringValue, String anotherStringValue) {
      AnotherTypeOfObject anotherTypeOfObject = runMethodA(myObject);
      YetAnotherTypeOfObject yetAnotherTypeOfObject = runMethodB(anotherTypeOfObject);
      runMethodC(yetAnotherTypeOfObject, aStringValue, anotherStringValue);
      runMethodD(yetAnotherTypeOfObject);
  }
  </code></pre>
  <p>And I have tried to run the main method as:</p>
  <pre class="lang-java prettyprint-override"><code>@Override
  @Async(&quot;threadPoolTaskExecutor&quot;)
  public ResponseEntity&lt;Void&gt; startProcess(List&lt;MyObject&gt; myObjectList) throws Exception {
      String aStringValue = myObject.getAStringValue();
      String anotherStringValue = myObject.getAnotherStringValue();
      myObjectList.forEach(myObject -&gt; runThreads(myObject, aStringValue, anotherStringValue));
  }
  </code></pre>
  <p>I still don't get the intended result of firing a few threads for the <code>runThreads(...) {}</code> method, so the processing is done in parallel.</p>
  <p>What am I missing here?</p>