在Java中检测OS睡眠和唤醒事件 [英] Detect OS Sleep and Wake Up events in Java

查看:181
本文介绍了在Java中检测OS睡眠和唤醒事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java程序是否有办法检测操作系统何时即将进入睡眠状态,或者失败了,至少检测到唤醒状态?

Is there a way for a Java program to detect when the operating system is about to go to sleep, or failing that, at least detecting a wake up?

实际的问题是,在特定的应用程序中,许多MySQL数据库操作在后台运行.在Windows机器上进行测试时,这些数据库事务将在睡眠/唤醒周期后中断,从而导致程序中出现大量错误情况.这些错误通常看起来像这样:

The actual problem is that in a particular application a number of MySQL database operations are run in the background. In testing on a Windows machine these database transactions are interrupted after a sleep/wake-up cycle causing a ton of error conditions in the program. These errors typically look something like this:

java.net.SocketException
MESSAGE: Software caused connection abort: recv failed

如果我们可以对即将入睡"事件做出反应,则可以尝试暂停后台操作以抢先解决该问题.不太理想的情况是,如果我们可以对刚刚醒来"的事件做出反应,那么我们至少可以抑制错误消息.

If we could react to a 'will sleep soon' event we could attempt to pause background operations preempting the problem. Less ideally if we could react to a 'just woke up' event we could at least suppress the error messages.

推荐答案

您可以通过定期比较当前系统时间和上一个系统时间来检测唤醒.

You could detect the wakeup by periodically comparing the current system time to the previous system time.

这是一个示例,它看起来像可以帮助您检测机器何时进入睡眠状态: http://www.codeguru.com/cpp/wp/system /messagehandling/article.php/c6907

here's an example that looks like it would help you detect when the machine is going to sleep: http://www.codeguru.com/cpp/w-p/system/messagehandling/article.php/c6907

这篇关于在Java中检测OS睡眠和唤醒事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆