问题描述
我使用while循环在程序中重复我的代码.我想每秒重复一次代码1000次.我该怎么办?
I use a while loop to repeat my codes in my program. I want to repeat a code 1000 times per second. How can I do this?
推荐答案
对于某些代码的固定延迟执行,使用 timer对象可能是更好的方法,例如 java.util.Timer 或 javax.swing.Timer ,甚至 AnimationTimer ,具体取决于您正在尝试完成.话虽如此,无法保证频率和1000/s(或1ms的周期)的频率非常快,并且java.util.Timer
和javax.swing.Timer
不能跟上.
For the fixed-delay execution of some code, it might be a better approach to use a timer object, such as java.util.Timer or javax.swing.Timer, or even AnimationTimer, depending on what you're trying to accomplish. This being said, it's not possible to guarantee a frequency and a frequency of 1000/s (or period of 1ms) is quite fast, and java.util.Timer
and javax.swing.Timer
won't be able to keep up.
另请参阅:
- How to use swing timers
- How to use Java.Util.Timer
- High Resolution Timer in Java 5
这篇关于如何设置每秒重复计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!