I have this:
ScheduledExecutorService scheduledThreadPool = Executors
.newScheduledThreadPool(5);
Then I start a task like so:
scheduledThreadPool.scheduleAtFixedRate(runnable, 0, seconds, TimeUnit.SECONDS);
I preserve the reference to the Future this way:
ScheduledFuture<?> scheduledFuture = scheduledThreadPool.scheduleAtFixedRate(runnable, 0, seconds, TimeUnit.SECONDS);
I want to be able to cancel and remove the future
scheduledFuture.cancel(true);
However this SO answer notes that canceling doesn't remove it and adding new tasks will end in many tasks that can't be GCed.
https://stackoverflow.com/a/14423578/2576903
They mention something about setRemoveOnCancelPolicy
, however this scheduledThreadPool
doesn't have such method. What do I do?