java 睡眠 唤醒,在Java中检测OS睡眠和唤醒事件

在Windows上运行的Java程序遇到操作系统休眠/唤醒后导致数据库操作中断,引发错误。为解决这个问题,可以尝试检测系统即将休眠的事件来暂停后台操作,或者在系统唤醒后抑制错误消息。可以通过比较当前系统时间和先前时间来检测唤醒事件。链接提供了一个可能的解决方案。
摘要由CSDN通过智能技术生成

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?

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.

Edit: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值