title: How to cancel ScheduledFuture task from another task and end gracefully?
body: <p>I am playing with <code>ScheduledExecutorService</code>. What I want to do is to start a simple ticker (one tick per second) and schedule another task later (after five seconds) which cancels the first one. And then block the main thread until everything finishes, which should be after both tasks finish (+- five seconds).</p>
  
  <p>This is my code:</p>
  
  <pre><code>ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
  Runnable tickTask = () -&gt; System.out.println("Tick");
  ScheduledFuture&lt;?&gt; scheduledTickTask = executor.scheduleAtFixedRate(tickTask, 0, 1, TimeUnit.SECONDS);
  Runnable cancelTask = () -&gt; scheduledTickTask.cancel(true);
  executor.schedule(cancelTask, 5, TimeUnit.SECONDS);
  executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
  </code></pre>
  
  <p>The problem which suprises me is that it BLOCKS as if there were still some running tasks. Why? The <code>cancelTask</code> should end immediately and the <code>scheduledTickTask</code> was just cancelled, so what is the problem?</p>