Windows 10 上的 Java Thread.sleep() 停止 S3 睡眠状态
有一个桌面应用程序使用Thread.sleep()来实现长时间(分钟或几小时)的延迟。从Windows XP到(至少)Windows 7,相同的应用程序一直运行良好。应用程序计算将来需要做某事的距离,然后点击Thread.sleep(msToWait)。这一直工作正常,即使系统在等待期间碰巧进入S3睡眠状态。
但是,从Windows 10开始,如果计算机在S3中,Thread.sleep()之后的代码不会“按时”执行。看起来机器在“msToWait”开始执行代码,加上机器在S3中的时间(现在不是100%确定这一点,但很可能)。
早期版本的Windows没有表现出这种行为;Thread.sleep() 等待了正确的时间之后的代码,无论睡眠状态如何。
测试已经在当前的JVM 1.7上进行了。
这是Windows 10错误吗?这是一个JVM错误吗?有解决方法吗?
其他数据:
开发了测试程序和程序。该过程是运行程序,使机器休眠约一分钟,然后唤醒机器并等待程序完成。
如果该程序在JVM版本:25.40-b25的Windows 10(报告为8)上运行,则失败:
C:\Users\Tester\Downloads>SleepTester.exe
Wed Apr 01 10:47:35 PDT 2015 Using default number of minutes: 5
Wed Apr 01 10:47:35 PDT 2015 You can use "SleepTester -minutes 10" to have it sleep for 10 minutes, for example.
Wed Apr 01 10:47:35 PDT 2015 JVM Version: 25.40-b25 Windows Version: Windows 8
Wed Apr 01 10:47:35 PDT 2015 The program will now wait for 5 minutes. Expect wrap-up at Wed Apr 01 10:52:35 PDT 2015
Wed Apr 01 10:53:38 PDT 2015 The system has come through the Thread.sleep(300000).
Wed Apr 01 10:53:38 PDT 2015 This should be a low number: 63589
Wed Apr 01 10:53:38 PDT 2015 This appears to be operating incorrectly...the expected sleep time has NOT been achieved.
Wed Apr 01 10:53:38 PDT 2015 Program is ending.
如果该进程在 Windows 7 上运行,则不会失败。
Wed Apr 01 17:12:18 EDT 2015 Java Runtime Version: 1.8.0_31-b13 JVM Version: 25.31-b07 Windows Version: Windows 7
Wed Apr 01 17:12:18 EDT 2015 The program will now wait for 6 minutes. Expect wrap-up at Wed Apr 01 17:18:18 EDT 2015
Wed Apr 01 17:18:18 EDT 2015 The system has come through the Thread.sleep(360000).
Wed Apr 01 17:18:18 EDT 2015 This should be a low number: 0
Wed Apr 01 17:18:18 EDT 2015 Program is ending.
这是测试程序:
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.");
}
}
我意识到我可以尝试各种其他方法来睡觉,但我认为,既然我经历了并记录了这一点,那么在尝试其他事情之前,我会把它贴在这里。
其他信息:此故障似乎也出现在 Windows 8 中(但不是 7 或更早版本)。
4/4/2019 更新
此问题在以下网址 [JDK-8221971][1] 的 bugs.java.com 可见。有一些早期的错误与该错误相关联。来自 linke JDK-8146730 bug 的评论:
17-04-2017 关于这个话题,有什么新闻吗?
04-04-2019 它已被推迟。这是一个低优先级和复杂的问题,没有人被积极地分配给它。
附加 2/17/2021
这可能是由于 Windows 操作系统编程以响应超时的方式发生了变化。即使直接使用Windows API,我也不确定如何实现使新旧Windows操作系统的行为相同的目标。
Windows XP、Windows Server 2003、Windows Vista、Windows 7、Windows Server 2008 和 Windows Server 2008 R2:dwMilliseconds 值包括在低功耗状态下花费的时间。例如,超时确实会在计算机处于睡眠状态时不断倒计时。
Windows 8、Windows Server 2012、Windows 8.1、Windows Server 2012 R2、Windows 10 和 Windows Server 2016:dwMilliseconds 值不包括在低功耗状态下花费的时间。例如,超时不会在计算机处于睡眠状态时不断倒计时。
https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobjectex