[英]Robot.delay(int) versus Thread.sleep(long)
I have a program whose only purpose is to drive a java.awt.Robot
in an infinite loop until an exit condition is met. 我有一个程序,其唯一目的是在无限循环中驱动java.awt.Robot
,直到满足退出条件。
The robot performs a number of actions in quick succession, which require a standard UI delay between them. 机器人快速连续执行许多操作,这需要它们之间的标准UI延迟。 For this, I use java.awt.Robot.setAutoDelay(int ms)
, which appears to be designed for precisely this purpose. 为此,我使用java.awt.Robot.setAutoDelay(int ms)
,它似乎是为了这个目的而设计的。
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. 我似乎可以选择使用java.awt.Robot.delay(int ms)
还是java.lang.Thread.sleep(long ms)
,我很好奇它们之间的区别是什么,我应该使用哪些。
My gut instinct was to keep all my operations in the same "place", and use java.awt.Robot.delay(int ms)
. 我的直觉是将我的所有操作保持在同一个“地方”,并使用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. 然而,在考虑了一下之后,我假设java.awt.Robot.delay(int ms)
会在Robot的操作堆栈上放置一个操作来完成,如果那些是我在无限循环中的唯一延迟,我可能非常快,并且不必要地为机器人生成一个荒谬的大型事件队列。
At that point, I checked the API for java.awt.Robot.delay(int ms)
, which told me the following: 那时,我检查了java.awt.Robot.delay(int ms)
的API ,它告诉我以下内容:
Sleeps for the specified time. 睡觉指定的时间。 To catch any
InterruptedException
s that occur,Thread.sleep()
may be used instead. 要捕获发生的任何InterruptedException
,可以使用Thread.sleep()
代替。
Having failed to gain any useful insight into the matter, I elected to ask you guys. 由于没有获得任何有用的洞察力,我选择问你们。
At first I would also assume that using delay()
would generate a large queue of events, in particular after reading the javadoc for waitForIdle()
: 起初我还假设使用delay()
会生成一个大的事件队列,特别是在读取waitForIdle()
的javadoc之后:
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! 但检查Robot.delay()
的源代码后,显示它基本上是一个Thread.sleep()
,在检查延迟时间为正且不超过1分钟后!
Abstract: both solutions are almost the same, use Thread.sleep()
for delaying longer than 1 minute or catching the InterruptedException. 摘要:两个解决方案几乎相同,使用Thread.sleep()
延迟超过1分钟或捕获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) 经过多年的Java编程后,我发现了如何在不必捕获InterruptedException的情况下进行睡眠(忽略创建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. 然而,在考虑了一下之后,我假设java.awt.Robot.delay(int ms)会在Robot的操作堆栈上放置一个操作来完成,如果那些是我在无限循环中的唯一延迟,我可能非常快,并且不必要地为机器人生成一个荒谬的大型事件队列。
Your fears are unfounded. 你的恐惧是没有根据的。 The delay(int)
method does exactly what the javadoc says it does. delay(int)
方法正如javadoc所说的那样。 It delays the calling thread rather than inserting a "delay for so long" event onto the Robot instance's queue. 它会延迟调用线程,而不是将“延迟这么长”的事件插入到Robot实例的队列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.