Search code examples
loopsif-statementwait

How to make a loop that loops 25 times, waits 5 minutes and then starts the next batch of 25? java


I know that I'll have a for loop that looks something like this. But other than that I have no idea for it to then start back up again :

for (int i = 0; i < 25; i++) {
    // my code here
}

Solution

  • To pause the thread that your loop is running in, use;

    Thread.sleep (num_milliseconds);
    

    If you want it to run every 5 minutes just use

    while (true){
        for (int i = 0; i<25; i++){
            // your code
            Thread.sleep(300000);
        }
    }