run() 方法的滞后性太强
private static int millis_per_tick = 1;
public void run()
{
// While not stop do
while(true)
{
Thread t = Thread.currentThread();
long startTime, timeTaken;
startTime = System.currentTimeMillis();
act();
timeTaken = System.currentTimeMillis() - startTime;
if(timeTaken < millis_per_tick )
{
try
{
Thread.sleep(millis_per_tick-timeTaken );
}
catch(InterruptedException ex)
{}
}
thread.sleep 方法不接受双精度值,仅接受浮点数和整数。但我需要一个小于 1 的值。
在太空中
public void act()
{
ListIterator<Body> iterator = bodies.listIterator();
while (iterator.hasNext())
{
Body body = iterator.next();
body.wirkenKraefte();
body.move();
}
spaceGUI.repaint();
private static int millis_per_tick = 1;
public void run()
{
// While not stop do
while(true)
{
Thread t = Thread.currentThread();
long startTime, timeTaken;
startTime = System.currentTimeMillis();
act();
timeTaken = System.currentTimeMillis() - startTime;
if(timeTaken < millis_per_tick )
{
try
{
Thread.sleep(millis_per_tick-timeTaken );
}
catch(InterruptedException ex)
{}
}
The thread.sleep method does not accept double values only float and integer. But i need a value under 1.
In Space
public void act()
{
ListIterator<Body> iterator = bodies.listIterator();
while (iterator.hasNext())
{
Body body = iterator.next();
body.wirkenKraefte();
body.move();
}
spaceGUI.repaint();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 Thread.sleep(long,int) API 中的 方法。
Check out the Thread.sleep(long,int) method in the API.
这是不可能的。 1ms 甚至超过 1ms (15.6ms),但这是无法保证的。
-> 我可以提高 Thread.Sleep 的分辨率吗?
This is not possible. 1ms is even more than 1ms (15.6ms) and this is not guranteed.
-> Can I improve the resolution of Thread.Sleep?
使用 Thread.sleep(long) 睡眠时间不能少于一毫秒 - 在许多平台上,您会发现即使睡眠一毫秒实际上也会睡眠更长的时间(例如 15 毫秒),具体取决于系统时钟的粒度。
换句话说,您基本上无法使用 Thread.sleep(long) 执行您想要执行的操作 - 并且尝试获得亚毫秒级精度的时间一开始就很麻烦。您可以尝试使用
System.nanoTime()
相反,它仅适用于经过的时间(在本例中很好) - 您可能想要考虑自旋锁等待合适的时间,而不是睡觉。在大多数情况下,即使尝试以每秒 1000 次调用的固定速率执行某些操作也是一个坏主意......这里的用例是什么?可能有更好的方法。
编辑:我刚刚注意到有
Thread.sleep(millis, nanos)
- 你可以尝试使用它并睡眠(比如) 0 毫秒和 100,000 纳秒。您应该意识到,在大多数平台上,这实际上并不能达到您想要的效果。您可以针对您的特定用例尝试它,但我个人不会抱太大希望。You can't sleep for less than a millisecond using
Thread.sleep(long)
- and on many platforms, you'll find that even sleeping for one millisecond will actually sleep for longer (e.g. 15 milliseconds) depending on the granularity of the system clock.In other words, you basically can't do what you're trying to do with
Thread.sleep(long)
- and trying to get a sub-millisecond precision time is troublesome in the first place. You can try usingSystem.nanoTime()
instead, which is only appropriate for elapsed times (so fine in this case) - and you might want to consider a spin-lock to wait until the right time, instead of sleeping.In most cases it's a bad idea to even try to execute something at a fixed rate of 1000 calls per second though... what's the use case here? There may be a better approach.
EDIT: I've just noticed that there is
Thread.sleep(millis, nanos)
- you could try using this and sleep for (say) 0 milliseconds and 100,000 nanoseconds. You should be aware that on most platforms this won't actually do what you want though. You can try it for your particular use case, but I wouldn't personally hold out too much hope.