Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Thread.sleep() on Windows 10 stops in S3 sleep status

842235May 11 2016 — edited May 11 2016

There's a desktop application that uses Thread.sleep() to achieve long (minutes or hours) delays. This same application has been working fine from Windows XP through (at least) Windows 7. The application calculates how far in the future it needs to do something, then hits a Thread.sleep(msToWait). This has been working fine, even if the system happens to go into S3 sleep state during the wait.

As of Windows 10 (EDIT: actually 8 exhibits the same problem), though, the code after Thread.sleep() does not execute "on time" if the machine has been in S3. It appears that the machine begins executing code at "msToWait" plus the time the machine has been in S3.

Earlier versions of Windows did not exhibit this behavior; code after Thread.sleep() waited the right amount of time, irrespective of sleep status.

Testing has been on the current JVM 1.7.

Is this a Windows 10 bug? Is this a JVM bug? Is there a work-around?

A test program and procedure were developed. The procedure is to 1) run the program, 2)cause the machine to sleep for about a minute, then 3)wake the machine and wait for the program to finish.

If this the program is run on Windows 10 (reporting as 8) with JVM Version: 25.40-b25, it fails. If the process is run on Windows 7, it does not fail.  By fail, I mean that the "This should be a low number:" value is approximately the number of ms the machine was sleeping, whereas on earlier versions of Windows, the value was zero (in my testing), but I'd be happy with anything less than a few thousand.

Here is the test program:

import java.util.Date;

public class SleepTester {

private static int mMinutes;
private static int mDefault = 5;

public static void main(String[] args) throws Exception {
  
for (int iArg = 0; iArg < args.length; ++iArg) {
  
if (args[iArg].equals("-minutes") && (iArg + 1) < args.length) {
  mMinutes
= Integer.parseInt(args[++iArg]);
  
}
  
}

  
if (mMinutes == 0) {
  mMinutes
= mDefault;
  
System.out.println(new Date() + " Using default number of minutes: " + mDefault);
  
System.out.println(new Date() + " You can use \"SleepTester -minutes 10\" to have it sleep for 10 minutes, for example.");
  
}

  
System.out.println(new Date() + " Java Runtime Version: " + System.getProperty("java.runtime.version") + " JVM Version: " + System.getProperty("java.vm.version") + " Windows Version: " + System.getProperty("os.name"));
  
long msDelay = mMinutes * 60 * 1000;
  
long wakePoint = new Date().getTime() + msDelay;
  
System.out.println(new Date() + " The program will now wait for " + mMinutes + " minutes. Expect wrap-up at " + new Date(wakePoint));
  
Thread.sleep(msDelay); // If the machine goes into S3 during this interval, it should not matter, as long as it's awake when it fires.
  
System.out.println(new Date() + " The system has come through the Thread.sleep(" + msDelay + "). ");
  
long msAccuracy = Math.abs(new Date().getTime() - wakePoint);
  
System.out.println(new Date() + " This should be a low number: " + msAccuracy);
  
if (msAccuracy > 1000) System.out.println(new Date() + " This appears to be operating incorrectly...the expected sleep time has NOT been achieved.");
  
System.out.println(new Date() + " Program is ending.");
}
}


Here is the question I posted in stackoverflow: http://stackoverflow.com/questions/29394222/java-thread-sleep-on-windows-10-stops-in-s3-sleep-status

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2016
Added on May 11 2016
0 comments
933 views