Using threads to wake me up - Check my code please

So I need to get up early tomorrow, and my alarm clock is broken, and my phone isn’t too reliable. Ive turned to java for the solution. I’ve created a little script which i hope will get me up. I do plan to have my computer on overnight, and I plan to do so with max volume on speakers, but im still not sure if Thread can handle such a long time. Which, according to python, is up to 28800000 milliseconds (if my math is correct). The Runtime.exec() will work, i’ve tested that.

Please check this.


public class Wake extends Thread {

	int hours = 6;
	
	public static void main(String[] args){
		new Wake().start();
	}
	
	public void run(){
		try{
			System.out.println("Starting...");
			Thread.sleep(hours*60*60*1000);
			Runtime runtime = Runtime.getRuntime();
			runtime.exec("firefox http://www.youtube.com/watch?v=y7tI1E6kp0o");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Thanks.

I’d do it like this :


long startTime = System.currentTimeMillis();
long timeToWake = hours*60*60*1000 ;

while(true)
{
try
  {
  Thread.sleep(1000);
  if (System.currentTimeMillis()-startTime > timeToWake) 
    break;
  }
catch (Exception e)
  {}
 
}

runtime.exec("firefox http://www.youtube.com/watch?v=CS9OO0S5w2k&ob=av3e");


It looks like it will work!
I havent really had the chance to use System.currentTimeMillis(), so can u guarantee that at some point it will just reset and return 0 later on?

Better safe than sorry.

If you’re planning to use your timer to wake up from cryogenic sleep, you definetely should worry about this.

System.currentTimeMillis() will overflow around the year 292278994, so if you wish to stay frozen after this year, you’ll need a little hack (for example a counter of overflows) to count time beyond that.

+1 for overflow counting.

I’d be paranoid of my DNS going flakey or the browser timing out or something right at wake time. Grab some local resource to your filesystem and play that.

The task scheduler in Windows can wake a machine from sleep, as can atd on some Linux distributions. Either way I’d check first to make sure RTC wakeups work before doing that.

…so, did you get up on time?

Lol, sure did!
Will be using this forevar!