确定ScheduledExecutorService下次触发的时间

Cam*_*olf 7 java android

有没有办法确定ScheduledExecutorService下次触发的当前毫秒或其他时间度量?

scheduleTaskExecutorUpdate = Executors.newSingleThreadScheduledExecutor();
Run Code Online (Sandbox Code Playgroud)

我有一个较长的运行时间ScheduledExecutorService(A)和较短的运行时间ScheduledExecutorService(B)我想更新一个TextView,显示ScheduledExecutorService(A)下一次触发时的倒计时.

old*_*inb 9

如果您跟踪ScheduledFuture执行程序安排的所有任务的s,那么是.这成为确定最小延迟直到下一个任务必须触发的问题,这应该是相当可靠的估计.

final Collection<ScheduledFuture<?>> futures = ...;
/* for each schedule, add it to the above collection */
...
final long delay = Collections.min(futures).getDelay(TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)

......或者,对于一项任务,您只需:

final ScheduledFuture<?> future = ...;
final long delay = future.getDelay(TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)

现在,如果你要做很多事情,有多项任务,我建议你保持一个DelayQueue.但是,您不能仅仅将ScheduledFutures抛入队列而不保持由定期任务引起的更改.幸运的是,这个类ScheduledThreadPoolExecutor应该通过它的decorateTask方法很好地处理它.

请注意,这意味着您需要ScheduledThreadPoolExecutor直接创建自己的.像下面这样的东西可能会起作用.

public class TrackingSingleThreadScheduledExecutor
    extends ScheduledThreadPoolExecutor {

  private final DelayQueue<ScheduledFuture<?>> tasks
      = new DelayQueue<RunnableScheduledFuture<?>>();

  public TrackingSingleThreadScheduledExecutor() {
    super(1);
  }

  public DelayQueue<? extends ScheduledFuture<V>> tasks() {
    return tasks;
  }

  public ScheduledFuture<V> next() {
    return tasks.peek();
  }

  protected <V> RunnableScheduledFuture<V> decorateTask
      (final Callable<V> callable, final RunnableScheduledFuture<V> task) {
    return new QueueAwareTask(task);
  }

  protected <V> RunnableScheduledFuture<V> decorateTask
      (final Runnable runnable, final RunnableScheduledFuture<V> task) {
    return new QueueAwareTask(task);
  }

  private final class QueueAwareTask<V> implements RunnableScheduledFuture<V> {

    private final RunnableScheduledFuture<V> inner;

    public QueueAwareTask(final RunnableScheduledFuture<V> inner) {
      this.inner = inner;
    }

    public boolean isPeriodic() {
      return inner.isPeriodic();
    }

    public long getDelay(final TimeUnit unit) {
      return inner.getDelay(unit);
    }

    public void run() {
      inner.run();
      if (queue.remove(inner) && inner.isPeriodic()
          && !inner.isCancelled()) {
        queue.add(inner);
      }
    }

    public int compareTo(final Delayed other) {
      return inner.compareTo(other);
    }

    public boolean cancel(final boolean mayInterruptIfRunning) {
      final boolean cancelled = inner.cancel(mayInterruptIfRunning);
      if (cancelled) {
        queue.remove(inner);
      }
      return cancelled;
    }

    public boolean isCancelled() {
      return inner.isCancelled();
    }

    public boolean isDone() {
      return inner.isDone();
    }

    public V get() throws InterruptedException, ExecutionException {
      return inner.get();
    }

    public V get(final long timeout, final TimeUnit unit)
        throws InterruptedException, ExecutionException {
      return inner.get(timeout, unit);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用如下.

final TrackingSingleThreadScheduledExecutor executor
    = new TrackingSingleThreadScheduledExecutor();
...
final long delay = executor.next().getDelay(TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)