ExecutorService接口(interface)的submit()方法返回Future接口(interface)(实际上我认为是FutureTask类),而ScheduledExecutorService接口(interface)的schedule()方法返回ScheduledFuture接口(interface)(实际上我不知道是哪个类,可能是同一个FutureTask)。
唯一的区别是 ScheduledFuture 也实现了 Delayed 接口(interface)并具有 getDelay 方法。
但是主要思想是什么 - 为什么是 ScheduledFuture 而不仅仅是 Future 与 ScheduledExecutorService 一起使用?
最佳答案
ScheduledFuture
还实现了 Delayed
接口(interface),该接口(interface)具有方法:
long getDelay(TimeUnit unit);
Javadoc 说:
Returns the remaining delay associated with this object, in the given time unit.
因此,它使您有可能获得此延迟
。所以实现这个接口(interface)的任务有这个delay属性,对于执行者来说这意味着在这个delay过去之前它不能被执行,而Future没有这个属性,可以一旦线程池(或您正在使用的任何执行器)准备好执行它,就会执行它。
关于java - ScheduledFuture 与 Future - 最大的区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509028/