【问题标题】:ScheduledFuture vs Future - what is the big difference?ScheduledFuture vs Future - 最大的区别是什么?
【发布时间】:2018-05-24 12:07:16
【问题描述】:

ExecutorService 接口的submit() 方法返回接口Future(其实我认为是FutureTask 类),而接口ScheduledExecutorService 的schedule() 方法返回ScheduledFuture 接口(其实我不知道是哪个类,可能同一个FutureTask)。

唯一不同的是ScheduledFuture也实现了Delayed接口,并且有getDelay方法。

但是主要的想法是什么 - 为什么是 ScheduledFuture 而不仅仅是 Future 与 ScheduledExecutorService 一起使用?

【问题讨论】:

    标签: java


    【解决方案1】:

    ScheduledFuture 也实现了Delayed 接口,它有方法:

    long getDelay(TimeUnit unit);
    

    Javadoc 说:

    返回与此对象关联的剩余延迟,在 给定时间单位。

    所以你可以得到这个delay。所以实现这个接口的任务有这个delay属性,对于执行者来说,这意味着在这个延迟过去之前它不能被执行,同时Future没有这样的属性并且可以在线程池(或者你是任何执行者)执行一次using) 已准备好执行它。

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2012-12-30
      相关资源
      最近更新 更多