java 中类超时_Java编写超时工具类

eb88e832ed3e1140da47a8090b1527bb.png

我们在开发过程中,在进行时间操作时,如果在规定的时间内完成处理的话,有可能会回到正确的结果。否则,就会被视为超时任务。此时,我们不再等待(不再执行)的时间操作,直接向调用者传达这个任务需要时间,被取消了。

1、说明

java已经为我们提供了解决办法。jdk1.5带来的并发库Future类可以满足这一需求。Future类中重要的方法有get()和cancel()。get()获取数据对象,如果数据没有加载,则在获取数据之前堵塞,cancel()取消数据加载。另一个get(timeout)操作表明,如果timeout时间内没有得到,就会失败回来,不会堵塞。

利用泛型和函数式接口编写一个工具类,可以让超时处理更方便,而不用到处写代码。

2、实例/**

* TimeoutUtil 

*

* @author lys

* @date 2021/2/25

*/

@Slf4j

@Component

@NoArgsConstructor

public class TimeoutUtil {

private ExecutorService executorService;

public TimeoutUtil(ExecutorService executorService) {

this.executorService = executorService;

}

/**

* 有超时限制的方法

*

* @param bizSupplier 业务函数

* @param timeout     超时时间,ms

* @return 返回值

*/

public  Result doWithTimeLimit(Supplier bizSupplier, int timeout) {

return doWithTimeLimit(bizSupplier, null, timeout);

}

/**

* 有超时限制的方法

*

* @param bizSupplier   业务函数

* @param defaultResult 默认值

* @param timeout       超时时间,ms

* @return 返回值

*/

public  Result doWithTimeLimit(Supplier bizSupplier, R defaultResult, int timeout) {

R result;

String errMsg = "Null value";

FutureTask futureTask = new FutureTask<>(bizSupplier::get);

executorService.execute(futureTask);

try {

result = futureTask.get(timeout, TimeUnit.MILLISECONDS);

} catch (InterruptedException | ExecutionException | TimeoutException e) {

errMsg = String.format("doWithTimeLimit执行超过%d毫秒,强制结束", timeout);

log.error(errMsg, e);

futureTask.cancel(true);

result = defaultResult;

}

return of(result, errMsg);

}

/**

* 随机耗时的测试方法

*/

private String randomSpentTime() {

Random random = new Random();

int time = (random.nextInt(10) + 1) * 1000;

log.info("预计randomSpentTime方法执行将耗时: " + time + "毫秒");

try {

Thread.sleep(time);

} catch (Exception e) {

}

return "randomSpentTime --> " + time;

}

public static void main(String[] args) throws Exception {

ExecutorService executorService = new ThreadPoolExecutor(1, 1,

0L, TimeUnit.MILLISECONDS,

new LinkedBlockingQueue(),

runnable -> {

Thread thread = new Thread(runnable);

// 以守护线程方式启动

thread.setDaemon(true);

return thread;

});

TimeoutUtil timeoutUtil = new TimeoutUtil(executorService);

for (int i = 1; i <= 10; i++) {

log.info("\n=============第{}次超时测试=============", i);

Thread.sleep(6000);

long start = System.currentTimeMillis();

String result = timeoutUtil.doWithTimeLimit(() -> timeoutUtil.randomSpentTime(), 5000).getOrElse("默认");

log.info("doWithTimeLimit方法实际耗时{}毫秒,结果:{}", System.currentTimeMillis() - start, result);

}

}

}

以上就是Java编写超时工具类的介绍,希望能对大家有所帮助。更多Java学习指路:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值