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

泪湿孤枕 提交于 2021-02-11 13:18:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!