问题
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?
回答1:
You can use Thread.sleep() or TimeUnit.SECONDS.sleep() which is a wrapper on Thread.sleep() for better readability.
来源:https://stackoverflow.com/questions/65548839/execute-an-instruction-every-x-seconds-in-a-for-loop-of-an-arraylist-using-java