Reputation: 5526
Is there a way to control the speed of execution of a loop ? I have a simulation that runs in a loop of 30000 steps. I want to visualise whats happening in that loop and if possible control the speed of execution while its running. Any ideas how i could do that ?
Upvotes: 2
Views: 2444
Reputation: 602
You can try to use 'Thread.sleep()' as the other guys said. But to "know" what happens in the loop I think you'd better debug it.. I think the worst case is to Print everything on the screen (wouldn't be so bad outside a loop, but considering 50+ loops it becomes impracticable.
Upvotes: 0
Reputation: 12390
you can put inside your loop Thread.sleep(latency);
where latency is in millis.
Upvotes: 1
Reputation: 62583
Put a Thread.sleep()
statement inside the loop. Beware though that you have to handle the exception.
for(int i = 0; i < 30000; i++) {
...
try {
Thread.sleep(100);
}
catch(InterruptedException e) {
// do something with e
}
}
Upvotes: 5
Reputation: 27486
You could add a sleep
to the loop to pause each iteration.
A better question though, is how are you visualizing this? I'm guessing you're watching the text flash by on the console... if that's the case you might want to consider outputting to files rather than the screen. That way you can read through the output at your leisure and you don't have to add artificial slowdowns to the program.
...but if by "visualise" it's an actual GUI thing, then yeah, the sleep might be better.
Upvotes: 6