How does the method "scheduleAtFixedRate" of the class ScheduledExecutorService work?
<p>I am trying an example using the method <code>scheduleAtFixedRate</code> of the class <code>ScheduledExecutorService</code>. The code is:</p>

<pre><code>ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
        IntStream.range(0, 4).forEach(i -&gt; service.scheduleAtFixedRate(() -&gt; {try {
            System.out.println(i + " task completed!");
        } catch (Exception e) {
            e.printStackTrace();
        }}, 
                0, 3, TimeUnit.SECONDS));
        service.shutdown();
</code></pre>

<p>The output is:</p>

<pre><code>1 task completed!
2 task completed!
0 task completed!
</code></pre>

<p>Why are not all 4 tasks executed?</p>

<p>If I omit the <code>service.shutdown();</code> then all the tasks are executed and repeatedly as well, but it leads to memory leak. According to the docs any before shutdown registered task should be executed.</p>

<p>Moreover, if I pause the current <code>Thread</code> for a while using <code>Thread.sleep()</code>, then the output contains all the tasks as:</p>

<pre><code>ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
        IntStream.range(0, 4).forEach(i -&gt; service.scheduleAtFixedRate(() -&gt; {try {
            System.out.println(i + " task completed!");
        } catch (Exception e) {
            e.printStackTrace();
        }}, 
                0, 3, TimeUnit.SECONDS));

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        service.shutdown();
</code></pre>

<p>Output:</p>

<pre><code>0 task completed!
3 task completed!
2 task completed!
1 task completed! 
</code></pre>

<p>Is this the expected behavior of the method <code>scheduleAtFixedRate</code>?</p>

<p><strong>Update:</strong></p>

<p>When I use <code>submit</code> instead of <code>scheduleAtFixedRate</code> without delay then the behavior comes close to the expected. Calling <code>shutdown()</code> allows all 4 tasks to complete while <code>shutdownNow()</code> only 3:</p>

<pre><code>ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
        IntStream.range(0, 4).forEach(i -&gt; service.submit(() -&gt; {

        System.out.println(i + " task completed");}));

        service.shutdown();
</code></pre>

<p>If I refrain from <code>ScheduledExcecutorService</code> and use <code>ExecutorService</code> instead as:</p>

<pre><code>ExecutorService service = Executors.newFixedThreadPool(4);
    IntStream.range(0, 4).forEach(i -&gt; service.submit(() -&gt; {

    System.out.println(i + " task completed");}));

     service.shutdown();
</code></pre>

<p>then all 4 tasks are always completed whether I use <code>shutdown()</code> of <code>shutdownNow()</code>.</p>

<p>It seems that the <code>ScheduledExecutorService</code> behaves a little bit different on the task submission and perhaps on <code>shutdown()</code> and <code>shutdownNow()</code>.</p>