Java 为什么Robot.delay(int-ms)限制为1分钟?
我在执行软件时遇到以下异常:Java 为什么Robot.delay(int-ms)限制为1分钟?,java,api-design,awtrobot,Java,Api Design,Awtrobot,我在执行软件时遇到以下异常: Exception in thread "main" java.lang.IllegalArgumentException: Delay must be to 0 to 60,000ms at java.awt.Robot.checkDelayArgument(Robot.java:544) at java.awt.Robot.delay(Robot.java:534) at com.company.Main.main(Main.java:1
Exception in thread "main" java.lang.IllegalArgumentException: Delay must be to 0 to 60,000ms
at java.awt.Robot.checkDelayArgument(Robot.java:544)
at java.awt.Robot.delay(Robot.java:534)
at com.company.Main.main(Main.java:10)
我感到惊讶的是,有一个睡眠时间限制,标准库异常消息语法错误/拼写错误(到0到?)。检查了delay()
方法的源代码后,我注意到它限制了等待时间,如异常所述:
/**
* Sleeps for the specified time.
* To catch any <code>InterruptedException</code>s that occur,
* <code>Thread.sleep()</code> may be used instead.
* @param ms time to sleep in milliseconds
* @throws IllegalArgumentException if <code>ms</code> is not between 0 and 60,000 milliseconds inclusive
* @see java.lang.Thread#sleep
*/
public synchronized void delay(int ms) {
checkDelayArgument(ms);
try {
Thread.sleep(ms);
} catch(InterruptedException ite) {
ite.printStackTrace();
}
}
private static final int MAX_DELAY = 60000;
private void checkDelayArgument(int ms) {
if (ms < 0 || ms > MAX_DELAY) {
throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
}
}
为什么要这样做?这似乎是糟糕的API设计。除了为您捕获冗余的InterruptedException
checked异常并同步调用之外,它还有什么用途?除了原始开发人员之外,没有人可以回答这个问题
您可以非常清楚地看到,它所做的只是调用Thread::sleep
,所以只需做同样的事情。您不需要调用Robot::delay
以下内容完全等效,没有任意限制
Robot r;
long sleepDuration = 60001;
synchronized (r) {
try {
Thread.sleep(sleepDuration);
} catch(InterruptedException ite) {
ite.printStackTrace();
}
}
这似乎是糟糕的API设计
这个班19岁。JDK中有很多糟糕的设计决策,尤其是在旧的东西中。它似乎毫无用处。有人故意这样设计,但我看不出有什么好处。我在文档中也找不到解释。我最初评论说我认为语法是正确的,因为我的大脑完全填满了句子中缺少的“set”。也许原作者也有同样的问题;)“让我吃惊的是,有一个睡眠时间限制,而且标准库异常消息有糟糕的语法/输入错误”还有很多东西要学,你仍然有