title: ScheduledExecutorService multiple threads in parallel
body: <p>I'm interested in using <code>ScheduledExecutorService</code> to spawn multiple threads for tasks if task before did not yet finish. For example I need to process a file every 0.5s. First task starts processing file, after 0.5s if first thread is not finished second thread is spawned and starts processing second file and so on. This can be done with something like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>    ScheduledExecutorService executor = Executors.newScheduledThreadPool(4)
      while (!executor.isShutdown()) {  
          executor.execute(task);
          try {
              Thread.sleep(500);
          } catch (InterruptedException e) {
              // handle
          }
      }
  </code></pre>
  
  <p>Now my question: Why I can't do it with <code>executor.scheduleAtFixedRate</code>? </p>
  
  <p>What I get is if the first task takes longer, the second task is started as soon as first finished, but no new thread is started even if executor has pool of threads. <code>executor.scheduleWithFixedDelay</code> is clear - it executes tasks with same time span between them and it doesn't matter how long it takes to complete the task. So probably I misunderstood <code>ScheduledExecutorService</code> purpose.</p>
  
  <p>Maybe I should look at another kind of executor? Or just use code which I posted here? Any thoughts?</p>