[英]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.我有一个 arraylist 包含几个“球”对象。 I want to browse this list to "shoot" each ball with x seconds between them.我想浏览此列表以“射击”每个球,它们之间有 x 秒。 Here is the code I have, I tried with the Timertask , System.currentMillis() / nano but I can't find a solution that works.这是我的代码,我尝试使用Timertask , System.currentMillis() / nano但我找不到有效的解决方案。
for(Ball ball: ballList) { // Browse the list
ball.setMove(true); // Launch ball
// Wait x seconds before sending new one
// ...
}
Does anyone have a solution?有没有人有办法解决吗?
You can use Thread.sleep() or TimeUnit.SECONDS.sleep() which is a wrapper on Thread.sleep() for better readability.您可以使用 Thread.sleep() 或 TimeUnit.SECONDS.sleep() ,它是 Thread.sleep() 的包装器,以获得更好的可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.