测量应用程序/线程花费的时间
我正在用 Java 编写一个模拟,其中物体在牛顿物理学下起作用。一个物体可能会受到一个力的作用,所产生的速度会导致它在屏幕上移动。模拟的本质意味着对象以离散的步骤移动,具体取决于动画循环的当前迭代和上一次迭代之间经过的时间;例如,
public void animationLoop() {
long prev = System.currentTimeMillis();
long now;
while(true) {
long now = System.currentTimeMillis();
long deltaMillis = now - prev;
prev = now;
if (deltaMillis > 0) { // Some time has passed
for (Mass m : masses) {
m.updatePosition(deltaMillis);
}
// Do all repaints.
}
}
}
如果动画线程以某种方式延迟,导致大量时间流逝(经典情况是在 Windows 下,单击并按住最小化/最大化可防止重新绘制),就会出现问题,这会导致对象以惊人的速度移动。我的问题:有没有办法确定动画线程中花费的时间而不是挂钟时间,或者有人可以建议一种解决方法来避免此问题?
到目前为止,我唯一的想法是通过某个上限来限制 deltaMillis 。
I am writing a simulation in Java whereby objects act under Newtonian physics. An object may have a force applied to it and the resulting velocity causes it to move across the screen. The nature of the simulation means that objects move in discrete steps depending on the time ellapsed between the current and previous iteration of the animation loop; e.g
public void animationLoop() {
long prev = System.currentTimeMillis();
long now;
while(true) {
long now = System.currentTimeMillis();
long deltaMillis = now - prev;
prev = now;
if (deltaMillis > 0) { // Some time has passed
for (Mass m : masses) {
m.updatePosition(deltaMillis);
}
// Do all repaints.
}
}
}
A problem arises if the animation thread is delayed in some way causing a large amount of time to ellapse (the classic case being under Windows whereby clicking and holding on minimise / maximise prevents a repaint), which causes objects to move at an alarming rate. My question: Is there a way to determine the time spent in the animation thread rather than the wallclock time, or can anyone suggest a workaround to avoid this problem?
My only thought so far is to contstrain deltaMillis
by some upper bound.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否考虑过使用计时器之类的东西而不是循环旋转?
这样,您至少可以保证在对象更新之间有一些延迟,因此您不应该像 while(true) 循环那样连续进行一堆重绘,并且如果线程延迟,您将不会立即重新绘制- 任务的执行,来自文档:“在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间来安排的。”
Have you considered using something like a timer instead of spinning in a loop?
This way you are guaranteed to at least have some delay between the update of your objects so you shouldn't have a bunch of repaints consecutively as with the while(true) loop, and if the thread is delayed you will not got an immediate re-execution of the task, from the docs: "In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution."
我发现
javax.swing.Timer.html
对此特别有帮助。下面是一个示例,它模拟了球形粒子和容器壁之间的弹性碰撞。附录:此相关方法可能有助于将模型与视图分离。一个单独的线程对系统的演变进行建模,而视图以固定的速率呈现模型的“快照”。
无论哪种情况,我都会限制速率以适应最慢的目标平台。
I found
javax.swing.Timer.html
particularly helpful for this. Here's an example that models elastic collisions among spherical particles and the walls of a container.Addendum: This related approach may help de-couple the model from the view. A separate thread models the system's evolution, while the view renders a "snapshot" of the model at a fixed rate.
In either case, I cap the rate to accommodate the slowest target platform.
您可能想阅读这篇文章,标题为“Java:使用 ThreadMXBean 获取线程时间”。
基本上,有一个类 ThreadMXBean 这使您能够获得在特定
Thread
上花费的时间。我还没有尝试过,但是这些方法(以及我提到的文章中的示例)看起来很有希望,所以我认为您将能够用它来完成您想要的事情。You might like to read this article entitled "Java: Getting Thread Time with ThreadMXBean".
Basically, there is a class ThreadMXBean that enables you to get time spent in particular
Thread
. I haven't tried that, but the methods (and examples from article I mentioned) look promising, so I think you will be able to accomplish what you want with this.我会使用动画库来制作动画,而不是重新发明轮子。这里有两个不错的选择:
I would use an animation library for doing the animation instead of reinventing the wheel. Here are two good choices: