Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:26:44+00:00 2026-06-10T16:26:44+00:00

Is there a way to determine the current millisecond or other time measure of

  • 0

Is there a way to determine the current millisecond or other time measure of when a ScheduledExecutorService is going to fire next?

scheduleTaskExecutorUpdate = Executors.newSingleThreadScheduledExecutor();

I have a longer running ScheduledExecutorService(A) and from a shorter running ScheduledExecutorService(B) I would like to update a TextView, display a countdown of when ScheduledExecutorService(A) is going to fire next.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:26:46+00:00Added an answer on June 10, 2026 at 4:26 pm

    If you keep track of the ScheduledFutures for all tasks scheduled with the executor, then yes. This becomes a problem of determining the minimum delay until the next task must fire, which should be a fairly reliable estimate.

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

    … or, for one task, you merely do:

    final ScheduledFuture<?> future = ...;
    final long delay = future.getDelay(TimeUnit.MILLISECONDS);
    

    Now, if you’re going to be doing it a lot, with mutiple tasks, I’d suggest you maintain a DelayQueue. However, you can’t merely throw the ScheduledFutures in the queue without maintaining the changes caused by periodic tasks. Luckily, the class ScheduledThreadPoolExecutor should handle this nicely via its decorateTask methods.

    Note this means you will need to create your ownScheduledThreadPoolExecutor directly. Something like the below might work.

    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);
        }
      }
    }
    

    Then, usage is as follows.

    final TrackingSingleThreadScheduledExecutor executor
        = new TrackingSingleThreadScheduledExecutor();
    ...
    final long delay = executor.next().getDelay(TimeUnit.MILLISECONDS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using chained functions, is there a way to determine if the current call
Is there a sensible way to determine whether the current user can access an
Is there any way to determine if process was invoked by current application? I'm
Is there a way to determine if the current user is a fan (now:
Is there a way to determine whether the current line is the last line
In a Windows XP system, is there a way to determine the current value
Is there any way to determine current filesystem location of executed class from the
Is there any way to determine if the current file is the root file
is there a way how to determine if the current mouse cursor is animated
Is there a way to determine if the current file is the one being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.