如何在Java中实现精确计时?我知道 Thread.sleep() 不准确并导致 InterruptedExceptions。我正在尝试制作一个物理模拟器,我想要准确的代码。到目前为止,这是我的代码:
public class FreeFall() {
public static void main(String[] args) {
double height = Double.parseDouble(args[0]);
double xi = height;
int counter = 0;
while (true) {
height = xi + 9.8 / * counter * counter;
System.out.println(height);
counter++;
Thread.sleep(1000);
}
}
}
Run Code Online (Sandbox Code Playgroud)