Louis
Louis

Reputation: 59

Execute an instruction every x seconds in a for loop of an arraylist using JAVA

I think this question has already been answered but I can't find the right solution to my problem.

I have a arraylist containing several "ball" objects. I want to browse this list to "shoot" each ball with x seconds between them. Here is the code I have, I tried with the Timertask, System.currentMillis() / nano but I can't find a solution that works.

for(Ball ball: ballList) { // Browse the list
   ball.setMove(true);     // Launch ball

   // Wait x seconds before sending new one
   // ...
}

Does anyone have a solution?

Upvotes: 0

Views: 110

Answers (1)

AjayLohani
AjayLohani

Reputation: 952

You can use Thread.sleep() or TimeUnit.SECONDS.sleep() which is a wrapper on Thread.sleep() for better readability.

Upvotes: 1

Related Questions