javax.swing.timer 的间隔是常数吗? [英] Is javax.swing.timer's interval constant?

查看:24
本文介绍了javax.swing.timer 的间隔是常数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javax.swing.Timer中,我们可以这样设置时间间隔:

In javax.swing.Timer, we are allowed to set the time interval as such:

int delay = 1000;    //update every 1000 millisecond
Timer t = new Timer(delay, listener);

根据上述内容,我预计每个间隔之间的时间延迟为 1000 毫秒.但是,当我在 Swing 应用程序中使用它时,每个间隔的延迟为 1014 到 1015.

With the above, I will expect the time delay between each interval to be 1000 millisecond. However I when I use it in a Swing application, the delay for each interval is 1014 to 1015.

当我将延迟设置为 1 时.测试的延迟为每个间隔 15 到 16 毫秒.

When I set the delay to 1. The tested delay is 15 to 16 milliseconds per interval.

我有两个关于上述计时器行为的问题:

I have 2 question with regards to the above Timer behaviour:

Q1:是什么导致我的间隔增加了额外的 14 到 15 毫秒?是运行 Swing 应用程序所需的开销"吗?

Q1: What is causing the additional 14 to 15 milliseconds being added to my interval? Is it the "overhead" needed to run the Swing application?

Q2:时间延迟是否能像我们在 Timer 构造函数或 timer.setDelay() 中声明的那样得到保证?我问这个是因为我知道 Thread.sleep(delay) 中的延迟是不能保证的,它随范围变化.那么,javax.swing.Timer 怎么样?

Q2: Will the time delay be guaranteed as what we have stated in the Timer constructor or timer.setDelay()? I ask this because I know the delay in Thread.sleep(delay) is not guaranteed, and it varies with a range. So, what about javax.swing.Timer?

推荐答案

定时器使用由操作系统实现的睡眠操作.当您在 Windows 上运行 32 位应用程序时,它使用 Win XP 模拟器,其工作方式与在 Window XP 上的工作方式相同.

The timer uses a sleep operation which is implemented by the operating system. When you run a 32-bit application on Windows, it uses the Win XP emulator which work the same way as it would on Window XP.

如果您使用 64 位 JVM,它将利用操作系统时钟的改进,它应该以 1 毫秒的分辨率运行.

If you use the 64-bit JVM which will take advantages of improvements in the OS clock, it should run with 1 milli-second resolution.

注意:ExecutorService 在 Java 5.0 中添加使用 System.nanoTime()LockSupport.parkNanos(n) 不应该有这个问题在于它使用高分辨率时钟.

Note: ExecutorService added in Java 5.0 uses System.nanoTime() and LockSupport.parkNanos(n) which shouldn't have this problem at it uses high resolution clock.

这篇关于javax.swing.timer 的间隔是常数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆