7

I have this simple code snippet :

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(10);

    Runnable r = new Runnable() {

        @Override
        public void run() {
            System.err.println(Calendar.getInstance().getTime());
        }
    }; 

    exec.scheduleAtFixedRate(r,0, 500, TimeUnit.MILLISECONDS);
}

On one machine, this code runs as expected. Console output :

  • Tue Jul 03 10:32:34 EEST 2012
  • Tue Jul 03 10:32:34 EEST 2012
  • Tue Jul 03 10:32:35 EEST 2012
  • Tue Jul 03 10:32:35 EEST 2012
  • Tue Jul 03 10:32:36 EEST 2012
  • Tue Jul 03 10:32:36 EEST 2012 ...

However on another machine, timestamp on the console shows ScheduledThreadPoolExecutor is getting late. By getting late I mean a couple of seconds :)

  • Tue Jul 03 10:32:34 EEST 2012
  • Tue Jul 03 10:32:37 EEST 2012
  • Tue Jul 03 10:32:40 EEST 2012
  • Tue Jul 03 10:32:44 EEST 2012
  • Tue Jul 03 10:32:50 EEST 2012
  • Tue Jul 03 10:32:55 EEST 2012 ...

Details of first machine on which code runs successfully :

i3 Windows 7 64 bit JRE 1.6.0.30

Details of second machine on which scheduling running late :

Dual Core Windows XP 32 bit JRE 1.6.0.18

I wonder why there is such a difference. Any suggestions ?

Thanks in advance.

uahakan
  • 576
  • 1
  • 6
  • 23
  • 1
    Can you give more details about the environment (os, jvm)? – assylias Jul 03 '12 at 07:48
  • The scheduling uses OS level system calls to perform the delay so if this is wrong, you have a low level problem. – Peter Lawrey Jul 03 '12 at 07:51
  • 2
    You might at least include the total CPU utilization of the two hosts. – Judge Mental Jul 03 '12 at 07:54
  • 2
    Do you get the same issue with a more recent version of the JRE on the second pc? – assylias Jul 03 '12 at 08:32
  • 1
    @JudgeMental CPU utilization during running tests is negligible. Funny thing is; I run this code in Eclipse on both machines, Eclipse versions are the same, when I resize the console window in Eclipse, scheduler seems to be "fired" so the timestamps are being updated as expected :) – uahakan Jul 03 '12 at 09:15

1 Answers1

1

actually the schedule timer is not accurate, it calculate time by cpu ticks. so if your machine load is too heavy, there may be some delay. check your second machine's load!

Cruis
  • 357
  • 2
  • 9