Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 1070 times)
0 Members and 1 Guest are viewing this topic.
Offline saucecode

Senior Newbie


Medals: 1



« Posted 2011-12-20 11: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: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2011-12-20 11: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 12: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: 48
Projects: 4
Exp: 8 years



« Reply #3 - Posted 2011-12-20 13: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 14:49:43 »

+1 for overflow counting.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2011-12-20 15: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 Devvie


Medals: 5
Projects: 1



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

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

Senior Newbie


Medals: 1



« Reply #7 - Posted 2011-12-23 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!