Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using threads to wake me up - Check my code please  (Read 788 times)
0 Members and 1 Guest are viewing this topic.
Offline saucecode

Senior Newbie


Medals: 1



« Posted 2011-12-20 12:43:41 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #1 - Posted 2011-12-20 12:50:28 »

I'd do it like this :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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");


Offline saucecode

Senior Newbie


Medals: 1



« Reply #2 - Posted 2011-12-20 13:27:45 »

I'd do it like this :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #3 - Posted 2011-12-20 14:08:18 »

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.

Offline ReBirth
« Reply #4 - Posted 2011-12-20 15:49:43 »

+1 for overflow counting.

Offline sproingie
« Reply #5 - Posted 2011-12-20 16:03:07 »

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.

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #6 - Posted 2011-12-23 04:12:52 »

...so, did you get up on time?
Offline saucecode

Senior Newbie


Medals: 1



« Reply #7 - Posted 2011-12-23 14:31:47 »

...so, did you get up on time?

Lol, sure did!
Will be using this forevar!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (108 views)
2013-05-17 21:29:12

alaslipknot (117 views)
2013-05-16 21:24:48

gouessej (146 views)
2013-05-16 00:53:38

gouessej (141 views)
2013-05-16 00:17:58

theagentd (154 views)
2013-05-15 15:01:13

theagentd (138 views)
2013-05-15 15:00:54

StreetDoggy (182 views)
2013-05-14 15:56:26

kutucuk (206 views)
2013-05-12 17:10:36

kutucuk (205 views)
2013-05-12 15:36:09

UnluckyDevil (212 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.169 seconds with 21 queries.