是Java中的方法重载示例。
(1)它包含两个名为 "sleepFixedTime" 的方法,一个是默认睡眠10秒的重载,另一个是允许自定义睡眠时间的重载。
(2)在自定义的重载方法中,如果 leftTime 小于 stepSeconds * 1000,则线程将睡眠 leftTime 毫秒,否则睡眠 stepSeconds * 1000 毫秒。
//方法重载:默认睡眠10秒
public static void sleepFixedTime(long timeout, long beforeTime)
throws InterruptedException, EMRCaseException {
sleepFixedTime(timeout, beforeTime, 10);
}
//方法重载:自定义睡眠时间(方法一定要大于10秒)
public static void sleepFixedTime(long timeout, long beforeTime, int stepSeconds)
throws InterruptedException, EMRCaseException {
long curTime = System.currentTimeMillis();
long leftTime = timeout * 1000 - (curTime - beforeTime);
if (leftTime <= 0) {
LOGGER.info(String.format("The left time is lower than 0"));
throw new EMRCaseException();
}
try {
if (leftTime < stepSeconds * 1000) {
Thread.sleep(leftTime);
} else {
Thread.sleep(stepSeconds * 1000);
}
} catch (InterruptedException e) {
LOGGER.info(String.format("Sleep fixed time for failed"));
throw e;
}
}