第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么 Robot.delay(int ms) 限制為 1 分鐘?

為什么 Robot.delay(int ms) 限制為 1 分鐘?

婷婷同學(xué)_ 2023-08-09 17:17:30
我在執(zhí)行軟件時(shí)遇到以下異常: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)令我驚訝的是,有一個(gè)睡眠時(shí)間限制,并且標(biāo)準(zhǔn)庫(kù)異常消息有錯(cuò)誤的語(yǔ)法/拼寫(xiě)錯(cuò)誤(to 0 to?)。檢查該方法的源代碼后delay(),我注意到它限制了等待時(shí)間,如異常所述:/** * 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 設(shè)計(jì)。InterruptedException除了為你捕獲多余的檢查異常并同步調(diào)用之外,它還有什么目的?
查看完整描述

1 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

除了原始開(kāi)發(fā)人員之外,沒(méi)有人可以回答這個(gè)問(wèn)題。


你可以很清楚地看到它所做的只是 call Thread::sleep,所以只需做同樣的事情即可。你不需要打電話Robot::delay。


以下是完全等價(jià)的,沒(méi)有任意限制


Robot r;

long sleepDuration = 60001;

synchronized (r) {

? ? try {

? ? ? ? Thread.sleep(sleepDuration);

? ? } catch(InterruptedException ite) {

? ? ? ? ite.printStackTrace();

? ? }

}

API 設(shè)計(jì)似乎很糟糕

查看完整回答
反對(duì) 回復(fù) 2023-08-09
  • 1 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)