I have thread working in background that calculates time before new day, waits for that time and then updates my UI. How to make program restart this thread when computer wakes up from sleep or hibernate?
-
With an OS hook, which is impossible in Java. Why are you trying to do low-level things with a high-level language? That's like trying to write an OS in Python. Sure, it'll be the greatest thing ever made, but good luck running it on any chip ever made. (If someone _has_ written an OS in Python and managed to get that to run _directly_ on their CPU, give me it. Now.) – Nic May 16 '15 at 13:31
-
I just need to calculate time before new day everytime computer turns on/wakes up. If you can suggest any other solution, I will gladly read it. – Alyona May 16 '15 at 13:43
-
...Okay, I did a little research, and it turns out I was wrong. Sorta. Assuming it's Windows (in which case you're invalidating the biggest benefit of Java) you could follow [this tutorial](http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7) with your compiled JAR file. It won't trigger when it gets out of sleep, however, just on startup. I'm still looking for a way to do that. – Nic May 16 '15 at 13:45
-
I know it is possible to listen to user logon/logoff events (see [here](http://stackoverflow.com/q/25727879/3080094)), so it should be possible to listen to wake up/hibernate events (see [here](http://stackoverflow.com/q/5440133/3080094) with what looks related [here](https://github.com/twall/jna/blob/master/contrib/platform/src/com/sun/jna/platform/win32/WinUser.java#L34)). I just do not know of any demo/sample code putting it all together. – vanOekel May 16 '15 at 16:36
1 Answers
You ought to be able to configure your system to auto-start your Java app when the system starts up. The question is how to react to the system hibernating.
It's not quite what you asked for, but I think this does the same thing: Within the Java app, make your sleep durations shorter (e.g. 5 minutes) and before you call Thread.sleep() calculate what time you think the thread should wake up. If the current time when you wake up is significantly different than what you expected, you can assume there was a system hibernate and you can adjust any time-to-new-day estimates. Of course, if you're sleeping at n-minute intervals, then you might just adjust your logic to sleep for n minutes or until midnight, whichever is sooner.
I ran this code on a virtual machine that I could suspend and it correctly identified when the VM was suspended:
long pause = 10000L;
long error = 100L;
while (true) {
long sleepTil = System.currentTimeMillis() + pause + error;
try { Thread.sleep(pause); } catch (InterruptedException e) { }
if (System.currentTimeMillis() > sleepTil) {
System.out.println("System was suspended");
} else {
System.out.println("System was not suspended.");
}
}

- 16,221
- 6
- 44
- 59
-
That is not exactly what I was after. In my case my thread is sleeping for a long time and I need to restart it right after system wakes up. Method that you wrote checks for time difference, so it can detect whether system was sleeping. There is not enough accuracy and if for checking "sleep" I need to rely on loop checking, than I just can get rid of long waiting period in my initial thread. What I needed is method that will trigger thread restart right after system wakes up and it shouldn't be another thread for loop checking, because there's no point in it then. – Alyona May 21 '15 at 09:20