Results 1 to 10 of 10
  1. #1
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Is thre a "every" in java?

    Is there something like "every" in java?
    I mean that for example it should print out a number every second.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Is thre a "every" in java?

    Create a Thread. Within the thread you create a loop. Inside the loop you print a number and then invoke Thead.sleep(1000).

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Is thre a "every" in java?

    You can even make a class implement the Iterable interface; it has to deliver an Iterator which can do what it wants (e.g. wait/sleep for a second before it returns something in its next() method). Iterables can be use in the enhanced for statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Is thre a "every" in java?

    Im new to java and i didnt really get it. Can you give me an example?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Is thre a "every" in java?

    Quote Originally Posted by ytrewqc View Post
    Im new to java and i didnt really get it. Can you give me an example?
    Sure, just a sec ...

    Java Code:
    import java.util.Iterator;
    
    class Every implements Iterable<Long> {
    
    	private long interval;
    	
    	public Every(long interval) { this.interval= interval; }
    
    	private class SecondsIterator implements Iterator<Long> {
    		
    		public boolean hasNext() { return true; }
    		
    		public Long next() {
    			
    			try {
    					Thread.sleep(interval);
    			}
    			catch (InterruptedException ie) { }
    			
    			return System.currentTimeMillis();
    		}
    		
    		public void remove() { }
    	}
    	
    	public Iterator<Long> iterator() {
    		return new SecondsIterator();
    	}
    	
    }
    public class T {
    
    	public static void main(String[] args) {
    		for (Long t : new Every(1000))
    			System.out.println("tick: "+t);
    	}
    }
    I don't think you understand it immediately but there's nothing wrong with reading the API documentation ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Is thre a "every" in java?

    Thanks! It really helped
    Quote Originally Posted by JosAH View Post
    Sure, just a sec ...

    Java Code:
    import java.util.Iterator;
    
    class Every implements Iterable<Long> {
    
    	private long interval;
    	
    	public Every(long interval) { this.interval= interval; }
    
    	private class SecondsIterator implements Iterator<Long> {
    		
    		public boolean hasNext() { return true; }
    		
    		public Long next() {
    			
    			try {
    					Thread.sleep(interval);
    			}
    			catch (InterruptedException ie) { }
    			
    			return System.currentTimeMillis();
    		}
    		
    		public void remove() { }
    	}
    	
    	public Iterator<Long> iterator() {
    		return new SecondsIterator();
    	}
    	
    }
    public class T {
    
    	public static void main(String[] args) {
    		for (Long t : new Every(1000))
    			System.out.println("tick: "+t);
    	}
    }
    I don't think you understand it immediately but there's nothing wrong with reading the API documentation ;-)

    kind regards,

    Jos

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Is thre a "every" in java?

    You can even make a class implement the Iterable interface;
    This approach will only work for console apps.

    For example you cause the Thread to sleep. This means you can't invoke the code in a GUI because you would cause the GUI to freeze.

    I would think managing the Thread yourself is more flexible as you could use the code in a console or GUI app.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Is thre a "every" in java?

    Quote Originally Posted by camickr View Post
    This approach will only work for console apps.

    For example you cause the Thread to sleep. This means you can't invoke the code in a GUI because you would cause the GUI to freeze.

    I would think managing the Thread yourself is more flexible as you could use the code in a console or GUI app.
    Not really; you can always 'lift away' that 'Every' thing from the EDT:

    Java Code:
    new Thread(new Runnable() {
       public void run() {
          for (Long t : Every(1000))
             // do something every second ...
       }
    }).start();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Is thre a "every" in java?

    Not really; you can always 'lift away' that 'Every' thing from the EDT:
    Good point!

    I did suggest you could create a separate Thread. I should learn to follow my own advice :)

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Is thre a "every" in java?

    Quote Originally Posted by camickr View Post
    Good point!

    I did suggest you could create a separate Thread. I should learn to follow my own advice :)
    :-) Well, it was a bare bones example; maybe a more complete class could do that 'lifting away' from the EDT itself and accept a Runnable for the real work, but on the other hand, I do like the idea of Iterable, i.e. the compiler understands those things ...

    kind regars,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •