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

  • Home
  • SEARCH
  • 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 6924859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:42:57+00:00 2026-05-27T10:42:57+00:00

There is a peculiarity that I encountered while using Java scheduled executors and was

  • 0

There is a peculiarity that I encountered while using Java scheduled executors and was wondering if what I experienced is normal.

I need to schedule tasks that execute at a predefined rate of 5 seconds. It is expected that these tasks will take longer than 5 seconds to execute from time to time, but when the time to run them goes below 5 seconds, the backed up list of tasks should run in quick succession to catch up. When running the tasks, it is important to know what the original scheduled execution time was (think scheduledExecutionTime() in java.util.TimerTask). Finally, I need to track the difference between scheduled time and actual time to identify when the schedule is “drifting” and by how much.

So far I have implemented all of this by using Java executors, and the following class illustrates the general idea:

public class ExecutorTest {
    public static final long PERIOD = 5000;

    public static void main(String[] args) {
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
                new Command(), 0, PERIOD, TimeUnit.MILLISECONDS);
    }

    private static final class Command implements Runnable {
        long timestamp = 0;

        public void run() {
            long now = System.currentTimeMillis();

            if (timestamp == 0) {
                timestamp = now;
            }

            // Drift is the difference between scheduled time and execution time
            long drift = now - timestamp;

            String format = "Ran at %1$tF %<tT,%<tL; drift: %2$dms";
            System.out.println(String.format(format, now, drift));

            timestamp += PERIOD;
        }
    }
}

Running the code listed above shows that the drift (which ideally should be as close to 0 as possible) fluctuates by as much as a few seconds, the result of which is that tasks as executed either prematurely or late. I have created a graph from the results of running this for about 150 minutes:

Java executor drift

So my first question is whether this is normal. My environment consists of 32 bit Windows XP and Java 1.5 update 21 (although Java 6 update 22 produces similar results).

The second question is whether there is a simple way to reduce the amount of drift. If I use a simple java.util.Timer or even just Thread.sleep(), the drift is non-existent.

Lastly, is there a better way of tracking the scheduled execution time when using scheduled executors?

  • 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-05-27T10:42:58+00:00Added an answer on May 27, 2026 at 10:42 am

    The scheduled executor service uses System.nanoTime which doesn’t drift as much as currentTimeMillis. Except if you are running on an XP system with more than one CPU socket. There is a bug in XP where the OS call System.nanoTime() uses is not consistent between sockets so as the thread switches which socket it is running on, you can expect to see this jumping around. (This is not a problem on Vista/7)

    On a Linux system with one socket your program reports 0 – 3 ms drift.

    Try this program.

    public static void main(String... args) throws Exception {
        long start = System.nanoTime();
        long time = start;
        while(time < start + 3e10) {
            long now = System.nanoTime();
            if (now < time || now > time + 50000) {
                System.out.println(now - time);
                now = System.nanoTime();
            }
            time = now;
        }
    }
    

    On an i7 system I see approx 10 jumps of up to 2 ms. If I use the machine I see more. What I expect you might see is large negative and positive timings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a component that I have been using since IE7 and had never
There a section in my code where I need to invert a matrix. That
There is a conversion process that is needed when migrating Visual Studio 2005 web
There is a field in my company's Contacts table. In that table, there is
There is a website called Gild.com that has different coding puzzles/challenges for users to
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
There is a class that's a fully fledged UIViewController and when that page loads
There is a security concept regarding the calls to the WCF service that I
There are many tutorials that talk about deleting index.php from the url. But I
There is a statement in the foudations of Qt Development book that goes as

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.