Ram
Ram

Reputation: 83

Iterate For loop for specific time line

I am new to java development, I have bit doubt on how to slow down the for loop. I have set of list counts, and i iterate those using for loop. i want to iterate the count for certain time limit. (ie) the iterating time between count 1 and two should be delayed for 2 sec. is this possible to do. Pl guide me on this

Upvotes: 0

Views: 127

Answers (3)

Gintas K
Gintas K

Reputation: 1478

Try using:

Thread.sleep(time); //time in milliseconds, in your case it is 2000

Upvotes: 1

shreyansh jogi
shreyansh jogi

Reputation: 2102

 public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {
            try {
                System.out.println(i);
                Thread.sleep(2000);
            } //System.out.println(stem[0]);
            catch (InterruptedException ex) {
                Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

Upvotes: 6

Bathsheba
Bathsheba

Reputation: 234685

The clearest way I can think of is to use

java.util.concurrent.TimeUnit.SECONDS.sleep(2);

Upvotes: 4

Related Questions