Stop a periodic task from within the task itself running in a ScheduledExecutorService
问题 Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService? Lets say, I have the following task: Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() { int count = 0; public void run() { System.out.println(count++); if (count == 10) { // ??? cancel self } } }, 1, 1, TimeUnit.SECONDS); From outside, it is easy to cancel via f.cancel(), but how can I stop the repetition at the specified place? (Passing the Future