《案例001:Java实现方法重载,并睡眠等待10秒》

是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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大大大钢琴

喜欢!就请他吃3块钱好吃的吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值