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

I have a program whose sole purpose is to drive a java.awt.Robotin an infinite loop until the exit condition is met.

The robot performs a series of actions in quick succession, which requires a standard delay between them. For this I use java.awt.Robot.setAutoDelay(int ms), which seems to be designed specifically for this purpose.

At the same time, I need to insert arbitrarily large delays to complete the operations. I seem to have a choice between using java.awt.Robot.delay(int ms)or java.lang.Thread.sleep(long ms), and I'm curious what the differences are between them and what I should use.

My brush instinct was to keep all my operations in the same β€œplace” and use java.awt.Robot.delay(int ms). However, thinking about this for a moment, I assumed that I would java.awt.Robot.delay(int ms)perform an operation on the robot stack to complete, and if these were my only delays in an infinite loop, I can very quickly and without need to generate an absurdly large queue of events for the robot.

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

Sleeps for the specified time. To catch anyone InterruptedExceptionthat occurs, can be used instead Thread.sleep().

Having not received a useful understanding of this question, I decided to ask you guys.

+3
2

, delay() , , javadoc waitForIdle():

, , ,

Robot.delay() , Thread.sleep(), , 1 !

: , Thread.sleep() 1 Exception.

Java , , InterruptedException ( )

+1

, , , java.awt.Robot.delay(int ms) , , .

. delay(int) , javadoc. , "delay for so long" Robot.

+1

Source: https://habr.com/ru/post/1753596/


All Articles