title: Java Spring - manage threads for sync requests
body: <p>I have Java web application with REST calls using SPRING.</p>
  
  <p>I want to control the number of threads the application is opening for the requests.</p>
  
  <p>So I added Thread config:</p>
  
  <pre><code>package myPackage;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.task.TaskExecutor;
  import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  
  @Configuration
  public class ThreadConfig {
      @Bean
      public TaskExecutor threadPoolTaskExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(1);
          executor.setMaxPoolSize(1);
          executor.initialize();
          return executor;
      }
  }
  </code></pre>
  
  <p>I'm using Sync service not Async, I tested it and it doesn't limit the threads handling the requests, it handles them all at the same time.</p>
  
  <p>What I was expecting is when I send 2 requests at a time - either the 2nd request will be thrown or it will wait until the 1st request will finish.</p>
  
  <p>I'm not implementing Thread in my application at all.
  This is the relevant code from my controller:</p>
  
  <pre><code>    @RestController
      public class Module1Controller {
          @RequestMapping(method = RequestMethod.GET, path = "/module1")
  InterruptedException {
          public Module1 Module1() throws InterruptedException {
              Date startDate = new Date();
              System.out.println("Thread #: " + Thread.currentThread().getId() + " Request received at: " + startDate);
              Thread.sleep(10000);
              Date endDate = new Date();
              long diff = endDate.getTime() - startDate.getTime();
              long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
              System.out.println("Thread #: " + Thread.currentThread().getId() + " thread released at: " + endDate + ", total seconds: " + seconds);
              return new Module1(new Clock());
          }
  </code></pre>
  
  <p>This is the console result:</p>
  
  <pre><code>Thread #: 34 Request received at: Sun Dec 17 10:16:20 IST 2017
  Thread #: 35 Request received at: Sun Dec 17 10:16:21 IST 2017
  Thread #: 34 thread released at: Sun Dec 17 10:16:30 IST 2017, total seconds: 10
  Thread #: 35 thread released at: Sun Dec 17 10:16:31 IST 2017, total seconds: 10
  </code></pre>
  
  <p>What am I missing here?</p>