To answer your title: Use submit
, not schedule
, or any related function. In particular look at the ExecutorService
interface instead of ScheduledExecutorService
, as its methods won't contain anything repeated.
To answer this, which I think is your actual question:
I only want to keep the "freshest" task and discard the older ones.
Note that when you schedule a task a ScheduledFuture<?>
is returned. You can cancel that when a newer task is submitted.