Java编写超时工具类实例讲解

在本篇内容里小编给大家分享的是一篇关于Java编写超时工具类实例讲解内容,有兴趣的朋友们可以学习参考下。

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

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); } } }

实例知识点扩展:

属性校验工具类

 /** * 校验对象中的属性。如果属性为null,抛异常。如果属性为字符串(空串或空格),抛异常。 * @author mex * @date 2019年4月18日 * @param e 对象 * @param fieldNames 属性名称数组 * @return void * @throws Exception */ public static  void validateAttr(E e, String[] fieldNames) throws Exception { if (null == e) { throw new Exception("请求对象为空"); } if (null == fieldNames) { return; } for (int i = 0; i 

到此这篇关于Java编写超时工具类实例讲解的文章就介绍到这了,更多相关Java编写超时工具类内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是Java编写超时工具类实例讲解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java