The problem is that Executors.newSingleThreadScheduledExecutor();
actually doesn't return a ScheduledThreadPoolExecutor
.
Source code in the Executors
class:
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
The Delegated...
classes (there's a DelegatedExecutorService
too) are just passing all the calls to the underlying executor, the ScheduledThreadPoolExecutor
in this case. The comments in the code suggest that the whole point of these classes is to hide all the non-interface methods that the underlying executor might have.
In any case, it is better practice anyway to use the interface rather than the class version of the object you are working on (List
and not ArrayList
, ScheduledExecutorService
and not ScheduledThreadPoolExecutor
).
If you absolutely need functionality available in ScheduledThreadPoolExecutor
and not in the ScheduledExecutorService
, you could use the constructor of ScheduledThreadPoolExecutor
to create an instance of it.