title: Multithreading in spring
body: <p>I am trying to get into spring multithreading and I have few questions. </p>
  
  <p>I have runnable method in ThreadRating class. Now I am not sure about the best way of using it.</p>
  
  <p><strong>option 1 I found:</strong></p>
  
  <pre><code>private void updateRating() {
          ExecutorService executor = Executors.newFixedThreadPool(10);
  
      for (int i = 0; i &lt; 10; i++) { // test
          // thread part
          Runnable worker = new ThreadRating(path, i, products.get(i), dao, fileHandler);
          executor.execute(worker);
      }
      executor.shutdown();
      try {
          executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
      } catch (InterruptedException e) {
          log.error("There was an error when ending threads");
          System.exit(1);
      }
      System.out.println("Finished all threads");
  }
  </code></pre>
  
  <p>This seem to be running fine. After the for loop, it waits until threads are finished and ends.</p>
  
  <p><strong>Second option I tried</strong></p>
  
  <pre><code>private TaskExecutor taskExecutor;
  
  public UpdateBO(TaskExecutor taskExecutor) {
      this.taskExecutor = taskExecutor;
  }
  
  private void updateRating() {
          for (int i = 0; i &lt; 10; i++) { // test
              Runnable worker = new ThreadRating(path, i, products.get(i), dao, fileHandler);
              taskExecutor.execute(worker);
          }
      // wait for threads to be finished before you go any further ??
          }
  </code></pre>
  
  <p>And in xml file I have</p>
  
  <pre><code>&lt;beans:bean id="taskExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt;
      &lt;beans:property name="corePoolSize" value="5" /&gt;
      &lt;beans:property name="maxPoolSize" value="10" /&gt;
      &lt;beans:property name="queueCapacity" value="25" /&gt;
      &lt;beans:property name="waitForTasksToCompleteOnShutdown" value="true" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="updateBO" class="UpdateBO"&gt;
      &lt;beans:constructor-arg ref="taskExecutor" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>And here are my questions:</p>
  
  <ol>
  <li>Is there any difference between these two options? Speed, memory, possible leaks? Or are they the same just written differently?</li>
  <li>Do I need to shutdown the pool when using webservice? I know that in the second option I don't have to, but is it the same when using webservice? </li>
  <li>When using second option - how should I tell to wait there until all jobs are finished? In the first option I just shut the pool down and wait to finish. But in the second option the code request is finished before the tasks are completed.</li>
  <li><code>executor.execute(worker);</code> - just to clarify. This is not actually creating a new thread, but it adds task to a queue and if the queue is full it waits right on this line in the code until there is free place?</li>
  </ol>
  
  <p>Thanks for helping me understand.</p>