Giannis
Giannis

Reputation: 5526

Loop execution speed control

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

Answers (4)

Breno Inojosa
Breno Inojosa

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

michal.kreuzman
michal.kreuzman

Reputation: 12390

you can put inside your loop Thread.sleep(latency); where latency is in millis.

Upvotes: 1

adarshr
adarshr

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

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

Related Questions