run() 方法的滞后性太强

发布于 2024-12-07 12:56:51 字数 926 浏览 9 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-12-14 12:56:51

查看 Thread.sleep(long,int) API 中的 方法。

使当前正在执行的线程休眠(停止执行)指定的毫秒数加上指定的纳秒数

Check out the Thread.sleep(long,int) method in the API.

Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.

我的鱼塘能养鲲 2024-12-14 12:56:51

这是不可能的。 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?

独守阴晴ぅ圆缺 2024-12-14 12:56:51

使用 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 using System.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文