469,353 Members | 2,259 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Delaying a loop

I have a program that contains a do-while loop, inside of which it
(re)calculates a array of labels XY[x][y], each one consisting of a
single digit, and print them forming an x by y rectangle. What I want to
do is slow the loop down so I can see the different formations of the
digits; so I tried

try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}

at the end of the loop, expecting to see the number formations changing
every second, like seeing a film frame by frame. Instead, however, there
was a loooooong delay, and then the loop reached almost instantaneously
its conclusion.

How can I stop the execution of the loop until I have time to see the
results in each pass?

Eustace

--
It ain't THAT, babe! - A radical reinterpretation
http://www.geocities.com/itaintme_babe/itaintme.html
Aug 2 '08 #1
7 8782
Eustace wrote:
I have a program that contains a do-while loop, inside of which it
(re)calculates a array of labels XY[x][y], each one consisting of a
single digit, and print them forming an x by y rectangle. What I want to
do is slow the loop down so I can see the different formations of the
digits; so I tried

try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}

at the end of the loop, expecting to see the number formations changing
every second, like seeing a film frame by frame. Instead, however, there
was a loooooong delay, and then the loop reached almost instantaneously
its conclusion.

How can I stop the execution of the loop until I have time to see the
results in each pass?

Eustace
Try flushing the io before the call to sleep. Your output is probably
being buffered.
i.e., if using System.out,
System.out.flush();
Aug 2 '08 #2
On 2008-08-02 15:39 Jon Bettinger wrote:
Eustace wrote:
>I have a program that contains a do-while loop, inside of which it
(re)calculates a array of labels XY[x][y], each one consisting of a
single digit, and print them forming an x by y rectangle. What I want
to do is slow the loop down so I can see the different formations of
the digits; so I tried

try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}

at the end of the loop, expecting to see the number formations
changing every second, like seeing a film frame by frame. Instead,
however, there was a loooooong delay, and then the loop reached almost
instantaneously its conclusion.

How can I stop the execution of the loop until I have time to see the
results in each pass?

Eustace

Try flushing the io before the call to sleep. Your output is probably
being buffered.
i.e., if using System.out,
System.out.flush();
Thanks. That makes sense. But I am not using System.out, the program
extends JFrame and I am using the setText command to change the JLabels
that have been added to the JPanel of the Container. How do I flush in
this case?

Eustace

--
It ain't THAT, babe! - A radical reinterpretation
http://www.geocities.com/itaintme_babe/itaintme.html
Aug 2 '08 #3
Eustace wrote:
On 2008-08-02 15:39 Jon Bettinger wrote:
>Eustace wrote:
>>I have a program that contains a do-while loop, inside of which it
(re)calculates a array of labels XY[x][y], each one consisting of a
single digit, and print them forming an x by y rectangle. What I want
to do is slow the loop down so I can see the different formations of
the digits; so I tried

try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}

at the end of the loop, expecting to see the number formations
changing every second, like seeing a film frame by frame. Instead,
however, there was a loooooong delay, and then the loop reached
almost instantaneously its conclusion.

How can I stop the execution of the loop until I have time to see the
results in each pass?

Eustace

Try flushing the io before the call to sleep. Your output is probably
being buffered.
i.e., if using System.out,
System.out.flush();

Thanks. That makes sense. But I am not using System.out, the program
extends JFrame and I am using the setText command to change the JLabels
that have been added to the JPanel of the Container. How do I flush in
this case?

Eustace
Hmmm, I'm not a swing expert, but you could try calling invalidate on
the component that changed.
Aug 4 '08 #4
On 2008-08-03 20:39 Jon Bettinger wrote:
Eustace wrote:
>On 2008-08-02 15:39 Jon Bettinger wrote:
>>Eustace wrote:
I have a program that contains a do-while loop, inside of which it
(re)calculates a array of labels XY[x][y], each one consisting of a
single digit, and print them forming an x by y rectangle. What I
want to do is slow the loop down so I can see the different
formations of the digits; so I tried

try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}

at the end of the loop, expecting to see the number formations
changing every second, like seeing a film frame by frame. Instead,
however, there was a loooooong delay, and then the loop reached
almost instantaneously its conclusion.

How can I stop the execution of the loop until I have time to see
the results in each pass?

Eustace
Try flushing the io before the call to sleep. Your output is
probably being buffered.
i.e., if using System.out,
System.out.flush();

Thanks. That makes sense. But I am not using System.out, the program
extends JFrame and I am using the setText command to change the
JLabels that have been added to the JPanel of the Container. How do I
flush in this case?

Eustace
Hmmm, I'm not a swing expert, but you could try calling invalidate on
the component that changed.
Thanks anyway. I prefer the newsgroups, but now I'll try the Sun forums.

--
Date Calculator with all-purpose JS code
http://www.geocities.com/emfril/js/datecalc.html
Aug 4 '08 #5
In comp.lang.java message <g7**********@aioe.org>, Mon, 4 Aug 2008
11:17:45, Eustace <em*@email.composted:

Sig:
>Date Calculator with all-purpose JS code
http://www.geocities.com/emfril/js/datecalc.html
Does it handle Ordinal Date, ISO Week Number, Julian/Gregorian, and
Easter Sunday? How about the Hebrew, Islamic, etc., calendars? Without
all of those, and more, it cannot be accurately described as all-
purpose.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 4 '08 #6
On 2008-08-04 14:35 Dr J R Stockton wrote:
In comp.lang.java message <g7**********@aioe.org>, Mon, 4 Aug 2008
11:17:45, Eustace <em*@email.composted:

Sig:
>Date Calculator with all-purpose JS code
http://www.geocities.com/emfril/js/datecalc.html

Does it handle Ordinal Date, ISO Week Number, Julian/Gregorian, and
Easter Sunday? How about the Hebrew, Islamic, etc., calendars? Without
all of those, and more, it cannot be accurately described as all-
purpose.
Dear Dr. Stockton.

We have crossed paths again. I most certainly do not intend to compete
with your unrivaled JavaScript subroutines. But mine have been useful
for some who still program in outdated languages (the owner of a QBasic
website was most thankful for sending him my subroutines, another
programmer once thanked me for helping him get a job - before 2000), and
I still prefer them as simpler when location, daylight savings time,
other calendars, and other considerations would unnecessarily complicate
the calculations.

One might argue, though, that the description "all-purpose JS code" is
not the best I could choose. I just intended it to mean that it is for
general, common, purposes and not for special ones - an all-purpose
product is not expected to be the best in any particular case, but than
your methods are an exception to that rule, being both "all-purpose" and
thorough. "Generic" might have been a better choice in my case, meaning
that it can be used in all computing languages, including those that do
not include a Date function. Anyway, I include occasionally the
signature with the description hoping to attract the curiosity of
someone who might find them useful for his/her purposes.

I certainly sense a feeling of jealousy, or maybe rather a kind of
competitiveness on your part, and I would like to express my sincere
gratitude for the deep sense of satisfaction that it has provided me. I
may insist that the variable name "epoch" would better apply to a 400
year period, and that the counting would more rationally start with year
1CE rather than 1970CE, but now such questions are of course of purely
academic interest; the alternate options have been established, and we
know very well that when such things get established it is very
difficult to change them. And anyway, personally I would be much more
interested to see the US adopt the metric system in my lifetime than to
have my opinions on the date functions prevail.

Cheers,

Eustace

--
It ain't THAT, babe! - A radical reinterpretation
http://www.geocities.com/itaintme_babe/itaintme.html
Aug 5 '08 #7
Eustace wrote:
On 2008-08-02 15:39 Jon Bettinger wrote:
Thanks. That makes sense. But I am not using System.out, the program
extends JFrame and I am using the setText command to change the JLabels
that have been added to the JPanel of the Container. How do I flush in
this case?

Eustace
What you probably want to do is something like this (pseudo code):

public class StuffDoer implements Runnable {
private void recalculate() {
//...
}

private void repaint() {
//...
}

public void run() {
Thread.sleep(30);
recalculate();
repaint();
SwingUtilities.invokeLater(this);
}
}
Aug 5 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Charles Alexander | last post: by
21 posts views Thread by Alo Sarv | last post: by
20 posts views Thread by pembeci | last post: by
5 posts views Thread by Martin Schou | last post: by
3 posts views Thread by Krakatioison | last post: by
32 posts views Thread by cj | last post: by
70 posts views Thread by hstagni | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.