Why doesn't Java Thread receive interrupt flag?
<p>I am trying to understand interrupting threads within an ExecutorService and I can't figure out why the following MyNeverEndingRunnable class doesn't get the interrupt signal. I have a class that implements Runnable and simply prints and waits in a loop until it is interrupted:</p>

<pre><code>class MyNeverEndingRunnable
    implements Runnable
{
    int count = 0;

    @Override
    public void run()
    {
        while (true)
        {
            System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
            try { Thread.sleep(5000L); } catch (Exception ignored) {}

            if (Thread.interrupted())
            {
                break;
            }
        }
    }
}
</code></pre>

<p>I spawn a few of these threads then call shutdownNow() on my ExecutorService which should call interrupt on each of the running threads but the below code continues to run forever:</p>

<pre><code>int threadCount = 5;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
Future[] threads = new Future[threadCount];
for (int k = 0; k &lt; threadCount; ++k)
{
    threads[k] = executorService.submit(new MyNeverEndingRunnable());
}

Thread.sleep(20000L);

executorService.shutdownNow();
while (!executorService.isShutdown()) Thread.sleep(1000L);
</code></pre>

<p>Does anyone know what I am doing wrong here?</p>