11 Replies Latest reply: Feb 15, 2012 11:02 AM by gimbal2 RSS

    Granularity of Timer seems variable in Windows 7

    ptoye
      Having recently moved to Windows 7 from XP, the behaviour of Timer objects seems to have changed. In XP (as I remember) the system clock was changed to 1ms, which gave good granularity. Now it seems to be 10 ms, which isn't accurate enough for my needs.

      This seems to happen for both javax.swing.timer and java.util.timer objects and in both Java 6 and 7 (but I'm still experimenting here).

      This happens unless I have my browser (Firefox) active and it's played a video clip, which seems to set the timer to 1 ms and not reset it afterwards.

      Any ideas on how to get java to give me 1ms (or less) timer granularity? Without running Firefox, that is.
        • 1. Re: Granularity of Timer seems variable in Windows 7
          jtahlborn
          There is great article on java and windows timers here: http://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
          • 2. Re: Granularity of Timer seems variable in Windows 7
            gimbal2
            ptoye wrote:
            Having recently moved to Windows 7 from XP, the behaviour of Timer objects seems to have changed. In XP (as I remember) the system clock was changed to 1ms, which gave good granularity. Now it seems to be 10 ms, which isn't accurate enough for my needs.
            No. The precision is changed to 1ms as long as there is a sleeping thread, not all the time. As soon as there is no sleeping thread anymore the precision is restored. This creates a problem as this causes the precision to be changed constantly in a threaded environment, which can cause the system clock to go out of sync. Known issue in at least older versions of Windows.
            >
            This seems to happen for both javax.swing.timer and java.util.timer objects and in both Java 6 and 7 (but I'm still experimenting here).
            Anything that is timing related, including Thread.sleep().
            This happens unless I have my browser (Firefox) active and it's played a video clip, which seems to set the timer to 1 ms and not reset it afterwards.
            That is because the timer precision is system wide; the video player changes the precision to 1ms which also makes it go into effect under Java. It will be reset as Windows itself will take care of that if the application doesn't do it. But it may stay that way until for example all instances of the browser are closed.
            Any ideas on how to get java to give me 1ms (or less) timer granularity? Without running Firefox, that is.
            Start an 'endless' sleeping daemon thread (sleep for MAX_LONG milliseconds is not endless, but it usually does the job) will make sure that the precision stays at 1ms throughout. At least that was true for Java 6 and below, I can't say if this is still the same under Java 7. Perhaps something has been changed.
            • 3. Re: Granularity of Timer seems variable in Windows 7
              ptoye
              There's a lot of good info there - thanks very much. It'll take some time to digest it all.
              • 4. Re: Granularity of Timer seems variable in Windows 7
                ptoye
                gimbal2 wrote:
                ptoye wrote:
                Having recently moved to Windows 7 from XP, the behaviour of Timer objects seems to have changed. In XP (as I remember) the system clock was changed to 1ms, which gave good granularity. Now it seems to be 10 ms, which isn't accurate enough for my needs.
                No. The precision is changed to 1ms as long as there is a sleeping thread, not all the time. As soon as there is no sleeping thread anymore the precision is restored. This creates a problem as this causes the precision to be changed constantly in a threaded environment, which can cause the system clock to go out of sync. Known issue in at least older versions of Windows.
                This seems to happen for both javax.swing.timer and java.util.timer objects and in both Java 6 and 7 (but I'm still experimenting here).
                Anything that is timing related, including Thread.sleep().
                This happens unless I have my browser (Firefox) active and it's played a video clip, which seems to set the timer to 1 ms and not reset it afterwards.
                That is because the timer precision is system wide; the video player changes the precision to 1ms which also makes it go into effect under Java. It will be reset as Windows itself will take care of that if the application doesn't do it. But it may stay that way until for example all instances of the browser are closed.
                Any ideas on how to get java to give me 1ms (or less) timer granularity? Without running Firefox, that is.
                Start an 'endless' sleeping daemon thread (sleep for MAX_LONG milliseconds is not endless, but it usually does the job) will make sure that the precision stays at 1ms throughout. At least that was true for Java 6 and below, I can't say if this is still the same under Java 7. Perhaps something has been changed.
                I'll obviously have to experiment with java 6 and 7 here. [Later] It's the same in both Java 6 and 7. Pity.

                But it's really annoying. It worked fine on my old machine with far less power running XP, so there's presumably been some change when Windows 7 (or maybe Vista, which passed me by) came in.

                And, while my code is running there's a thread with a recurrent timer which isn't switched off, so I'd have thought that the granularity would stay at 1ms. I use scheduleAtFixedRate for the java.util timer, and setRepeats(true) for the swing timer.

                Edited by: ptoye on 12-Feb-2012 09:14 - added comment about Java 6 & 7
                • 5. Re: Granularity of Timer seems variable in Windows 7
                  sabre150
                  You may think it stupid but add the following at the start of your main method.
                  /*
                           * See http://bugs.sun.com/view_bug.do?bug_id=6435126
                           */
                          new Thread()
                          {
                  
                              {
                                  this.setDaemon(true);
                                  this.start();
                              }
                  
                              public void run()
                              {
                                  while (true)
                                  {
                                      try
                                      {
                                          Thread.sleep(Integer.MAX_VALUE);
                                      } catch (InterruptedException ex)
                                      {
                                      }
                                  }
                              }
                          };
                  • 6. Re: Granularity of Timer seems variable in Windows 7
                    gimbal2
                    Yes that is what I was describing :)
                    • 7. Re: Granularity of Timer seems variable in Windows 7
                      ptoye
                      Having read some of the links referenced here I don't think it's stupid at all! Just a Microsoft bug/oddity. And it seems to be working. I was slowly getting there myself (this is a low-priority project at the moment), but your code is really useful.

                      Many thanks.
                      • 8. Re: Granularity of Timer seems variable in Windows 7
                        ptoye
                        Sabre150's solution works and gets round the Microsoft bug/oddity. Of course, it shouldn't be necessary....
                        • 9. Re: Granularity of Timer seems variable in Windows 7
                          gimbal2
                          ptoye wrote:
                          Sabre150's solution works and gets round the Microsoft bug/oddity. Of course, it shouldn't be necessary....
                          It hasn't much to do with Microsoft. Setting the timer precision very high is a strain on the OS as it has to interrupt more often. The idea to set the precision high only when you really need it (video, games, ...) is not that strange to me. I just find Sun's "solutions" around it a bit oddly chosen.
                          • 10. Re: Granularity of Timer seems variable in Windows 7
                            ptoye
                            Fair enough about the interrupt rate, but in these days of high-powered processors it's less worrisome than in the olden days (which I well remember, having written device drivers back in the 1970s).

                            On Sun's solution - can you think of a better one, given that clock rates are global, and it's really up to the OS to keep track of what precision is needed. I'm not a Windows (or any modern OS) API expert, so don't know if there's an easy way of asking the OS to increase its clock rate. And as for poking around the MS site trying to find information - watching paint drying is (a) quicker, and (b) less frustrating.
                            • 11. Re: Granularity of Timer seems variable in Windows 7
                              gimbal2
                              ptoye wrote:
                              Fair enough about the interrupt rate, but in these days of high-powered processors it's less worrisome than in the olden days (which I well remember, having written device drivers back in the 1970s).
                              Oh you are correct, I was more referring to the time where this behavior was introduced - many many years ago when snow was whiter and computers were slower :) Nowadays you have the high precision timer. But yeah, even if you have access to that high precision timer through the System class, Java's sleep mechanism still works on the 'old' timer routines.