Robot.delay(int) 与 Thread.sleep(long)

发布于 2024-09-08 06:10:38 字数 831 浏览 10 评论 0原文

我有一个程序,其唯一目的是在无限循环中驱动 java.awt.Robot 直到满足退出条件。

机器人快速连续执行多个动作,这需要它们之间有标准的 UI 延迟。为此,我使用 java.awt.Robot.setAutoDelay(int ms),它似乎正是为此目的而设计的。

然而,在其他时候,我需要插入任意长的延迟才能完成操作。我似乎可以选择使用 java.awt.Robot.delay(int ms) 或 java.lang.Thread.sleep(long ms) ,我很好奇什么它们之间的区别是,以及我应该使用哪些。

我的直觉是将所有操作保持在同一个“位置”,并使用 java.awt.Robot.delay(int ms)。然而,思考片刻后,我假设 java.awt.Robot.delay(int ms) 会将一个操作放在机器人的操作堆栈上来完成,如果这些是我唯一的操作由于无限循环中的延迟,我可能会非常快速且不必要地为机器人生成一个大得荒谬的事件队列。

此时,我检查了 java.awt.Robot.delay(int ms) 的 API,它告诉我以下内容:

休眠指定的时间。要捕获发生的任何 InterruptedException,可以改用 Thread.sleep()

由于未能对此事获得任何有用的见解,我决定向你们询问。

I have a program whose only purpose is to drive a java.awt.Robot in an infinite loop until an exit condition is met.

The robot performs a number of actions in quick succession, which require a standard UI delay between them. For this, I use java.awt.Robot.setAutoDelay(int ms), which appears to be designed for precisely this purpose.

At other times, however, I need to insert arbitrarily long delays for operations to complete. I appear to have a choice between using java.awt.Robot.delay(int ms) or java.lang.Thread.sleep(long ms), and am curious what the differences between them are, and which I should use.

My gut instinct was to keep all my operations in the same "place", and use java.awt.Robot.delay(int ms). However, after thinking about it for a moment, I assumed that java.awt.Robot.delay(int ms) would put an operation on the Robot's stack of operations to complete, and if those were my only delays in an infinite loop, I may very quickly, and needlessly, generate an absurdly large queue of events for the Robot.

At that point, I checked the API for java.awt.Robot.delay(int ms), which told me the following:

Sleeps for the specified time. To catch any InterruptedExceptions that occur, Thread.sleep() may be used instead.

Having failed to gain any useful insight into the matter, I elected to ask you guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谜兔 2024-09-15 06:10:38

首先,我还假设使用 delay() 会生成大量事件队列,特别是在阅读 waitForIdle() 的 javadoc 后:

等待事件队列中当前的所有事件都已处理完毕

,但检查 Robot.delay() 的源代码表明它基本上是一个 Thread.sleep(),之后检查延迟时间是否为正且不超过 1 分钟!

摘要:两种解决方案几乎相同,使用 Thread.sleep() 来延迟超过 1 分钟或捕获 InterruptedException。

经过多年的 Java 编程,我发现了如何在不捕获 InterruptedException 的情况下休眠(不考虑创建机器人的开销)

At first I would also assume that using delay() would generate a large queue of events, in particular after reading the javadoc for waitForIdle():

Waits until all events currently on the event queue have been processed

but checking the source code of Robot.delay() shows that it basically is a Thread.sleep(), after checking that the delay time is positive and not more than 1 minute!

Abstract: both solutions are almost the same, use Thread.sleep() for delaying longer than 1 minute or catching the InterruptedException.

after years of programming with Java I found how to sleep without having to catch the InterruptedException (disregarding the overhead of creating a Robot)

瞎闹 2024-09-15 06:10:38

然而,考虑了一下之后,我假设 java.awt.Robot.delay(int ms) 会将一个操作放在机器人的操作堆栈上来完成,如果这些是我在无限中唯一的延迟循环,我可能会非常快速且不必要地为机器人生成一个大得离谱的事件队列。

你的恐惧是没有根据的。 delay(int) 方法完全按照 javadoc 的说明执行操作。它延迟调用线程,而不是将“延迟这么长时间”事件插入到 Robot 实例的队列中。

However, after thinking about it for a moment, I assumed that java.awt.Robot.delay(int ms) would put an operation on the Robot's stack of operations to complete, and if those were my only delays in an infinite loop, I may very quickly, and needlessly, generate an absurdly large queue of events for the Robot.

Your fears are unfounded. The delay(int) method does exactly what the javadoc says it does. It delays the calling thread rather than inserting a "delay for so long" event onto the Robot instance's queue.

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