Thread: Execute code after X amount of time?

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 Execute code after X amount of time? 
    Belgium

    Join Date
    Dec 2012
    Posts
    214
    Thanks given
    23
    Thanks received
    21
    Rep Power
    49
    Iv'e made this small program that executes a line of code after a certain amount of time.
    But I believe that this is a horrible example. Could anyone help me on improving this?


    Code:
     class hello { 
    	
    	static long time = System.currentTimeMillis();
    	static long wait = time + 1000;
    	static long timer = 0;
    	public static void main (String[] args ) throws InterruptedException{ 
    		System.out.println(print(true));
    		while(System.currentTimeMillis() <= wait){
    			timer = System.currentTimeMillis();
    		}
    		if(timer >= wait){
    			System.out.println(print(false));
    		}
    	}
    	public static String print(boolean show){
    		if(show){
    			return "x";
    		}
    		else{
    			return "y";
    		}
    	}
    }
    Reply With Quote  
     

  2. #2  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,884
    Thanks given
    509
    Thanks received
    1,905
    Rep Power
    5000
    Code:
    class Hello {
        public static void main(String[] args) {
            System.out.println("x");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("y");
        }
    }
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Belgium

    Join Date
    Dec 2012
    Posts
    214
    Thanks given
    23
    Thanks received
    21
    Rep Power
    49
    Quote Originally Posted by S747 View Post
    Code:
    class Hello {
        public static void main(String[] args) {
            System.out.println("x");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("y");
        }
    }
    Thanks a lot.
    Reply With Quote  
     

  5. #4  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,884
    Thanks given
    509
    Thanks received
    1,905
    Rep Power
    5000
    this isn't homework is it
    Reply With Quote  
     

  6. #5  
    Belgium

    Join Date
    Dec 2012
    Posts
    214
    Thanks given
    23
    Thanks received
    21
    Rep Power
    49
    Quote Originally Posted by S747 View Post
    this isn't homework is it
    Nah, I actually try to learn java in my spare time.
    Reply With Quote  
     

  7. #6  
    Sexy boi <3

    ByteValue's Avatar
    Join Date
    May 2012
    Age
    24
    Posts
    831
    Thanks given
    11
    Thanks received
    80
    Rep Power
    80
    Try avoiding static variables in this case, because you use them only in your method, not as global.
    Reply With Quote  
     

  8. #7  
    I'm secretly a carrot named 3arl.

    Mainframe's Avatar
    Join Date
    Jun 2012
    Age
    22
    Posts
    1,428
    Thanks given
    542
    Thanks received
    214
    Rep Power
    147
    You're really adding more than what was necessary.

    Btw I'd suggest if you're just starting out with Java not to rush into things like this, stick to simplier programs like maybe a calculator that can add more than 2 integers at a time or something. Something that is challenging for those inexperienced but nothing too hard.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Useful Plugins for Eclipse:
    • [Only registered and activated users can see links. ]
    • [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #8  
    Registered Member
    whac's Avatar
    Join Date
    Nov 2011
    Posts
    176
    Thanks given
    35
    Thanks received
    84
    Rep Power
    245
    Quote Originally Posted by Hugh Jass View Post
    You're really adding more than what was necessary.

    Btw I'd suggest if you're just starting out with Java not to rush into things like this, stick to simplier programs like maybe a calculator that can add more than 2 integers at a time or something. Something that is challenging for those inexperienced but nothing too hard.
    The OP's program is pretty simple already.
    Reply With Quote  
     

  10. #9  
    Belgium

    Join Date
    Dec 2012
    Posts
    214
    Thanks given
    23
    Thanks received
    21
    Rep Power
    49
    Quote Originally Posted by Hugh Jass View Post
    You're really adding more than what was necessary.

    Btw I'd suggest if you're just starting out with Java not to rush into things like this, stick to simplier programs like maybe a calculator that can add more than 2 integers at a time or something. Something that is challenging for those inexperienced but nothing too hard.
    I knew that there were unnecessary parts in it, it's a basic design of calling a method after x amount of time. I'm past basic calculator that's why I slowly move on
    But I apriciate your concern.
    Reply With Quote  
     

  11. #10  
    Father Of Lies


    Join Date
    May 2012
    Age
    22
    Posts
    1,216
    Thanks given
    267
    Thanks received
    289
    Rep Power
    242
    instead of declaring a method static to call it from a static context, make an object of the class in the main method and call it as such:

    Code:
    
    
     class hello { 		private long time = System.currentTimeMillis();	private long wait = time + 1000;	private long timer = 0;	public static void main (String[] args ) throws InterruptedException{ hello h = new hello(); //declaring a new object		System.out.println(h.print(true));		while(System.currentTimeMillis() <= h.wait){			h.timer = System.currentTimeMillis();		}		if(h.timer >= wait){			System.out.println(h.print(false));		}	}	private String print(boolean show){		if(show){			return "x";		}		else{			return "y";		}	}}
    Also made a few things private, as they are not accessed by other classes. And do they really need to be longs?
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 17
    Last Post: 01-17-2013, 06:07 AM
  2. Replies: 7
    Last Post: 01-05-2011, 01:19 PM
  3. [508] Has Item Amount Code
    By Bulby Strife in forum Tutorials
    Replies: 5
    Last Post: 11-22-2008, 06:47 PM
  4. Replies: 47
    Last Post: 05-01-2008, 02:25 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •