Java 延迟/等待
如何将 while 循环延迟到 1 秒间隔,而不减慢其运行的整个代码/计算机的速度到一秒延迟(只是一个小循环)。
How do I delay a while loop to 1 second intervals without slowing down the entire code / computer it's running on to the one second delay (just the one little loop).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来你的循环在主线程上运行,如果你在该线程上睡眠,它将暂停应用程序(因为只有一个线程已暂停),为了克服这个问题,你可以将此代码放入并行运行的新
Thread
It seems your loop runs on Main thread and if you do
sleep
on that thread it will pause the app (since there is only one thread which has been paused), to overcome this you can put this code in newThread
that runs parallely我延迟循环的简单方法。
在未能遵循 stackoverflow 的标准之后,我已经将代码放在这里了。
My simple ways to delay a loop.
I already put the codes here after failing to follow the stackoverflow's standards.
正如 Jigar 所指出的,您可以使用另一个线程来完成可以独立于其他线程运行、睡眠等的工作。 java.util.Timer 类也可能对您有所帮助,因为它可以为您执行定期任务,而无需您进行多线程编程。
As Jigar has indicated you can use another Thread to do work which can operate, sleep etc independently of other Threads. The
java.util.Timer
class might help you as well since it can perform periodic tasks for you without you having to get into multithreaded programming.