Results 1 to 10 of 10
Thread: Is thre a "every" in java?
- 11-01-2011, 02:49 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
- 11-01-2011, 04:05 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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).
- 11-01-2011, 04:19 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-01-2011, 04:38 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Re: Is thre a "every" in java?
Im new to java and i didnt really get it. Can you give me an example?
- 11-01-2011, 04:52 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: Is thre a "every" in java?
Sure, just a sec ...
I don't think you understand it immediately but there's nothing wrong with reading the API documentation ;-)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); } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-01-2011, 04:58 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
- 11-01-2011, 05:21 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Is thre a "every" in java?
This approach will only work for console apps.You can even make a class implement the Iterable interface;
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.
- 11-01-2011, 06:04 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: Is thre a "every" in java?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-01-2011, 06:17 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Is thre a "every" in java?
Good point!Not really; you can always 'lift away' that 'Every' thing from the EDT:
I did suggest you could create a separate Thread. I should learn to follow my own advice :)
- 11-01-2011, 07:28 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: Is thre a "every" in java?
:-) 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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks