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

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

1、说明

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

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

2、实例

/** * TimeoutUtil <br> * * @author lys * @date 2021/2/25 */@Slf4j@Component@NoArgsConstructorpublic class TimeoutUtil { private ExecutorService executorService; public TimeoutUtil(ExecutorService executorService) {   this.executorService = executorService; } /**  * 有超时限制的方法  *  * @param bizSupplier 业务函数  * @param timeout   超时时间,ms  * @return 返回值  */ public <R> Result<R> doWithTimeLimit(Supplier<R> bizSupplier, int timeout) {   return doWithTimeLimit(bizSupplier, null, timeout); } /**  * 有超时限制的方法  *  * @param bizSupplier  业务函数  * @param defaultResult 默认值  * @param timeout    超时时间,ms  * @return 返回值  */ public <R> Result<R> doWithTimeLimit(Supplier<R> bizSupplier, R defaultResult, int timeout) {   R result;   String errMsg = "Null value";   FutureTask<R> 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>(),       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 <E> void validateAttr(E e, String[] fieldNames) throws Exception {    if (null == e) {      throw new Exception("请求对象为空");    }    if (null == fieldNames) {      return;    }    for (int i = 0; i < fieldNames.length; i++) {      String fieldName = fieldNames[i];      Field field = e.getClass().getDeclaredField(fieldName);      String typeName = field.getGenericType().getTypeName();      field.setAccessible(Boolean.TRUE);      Object fieldValue = field.get(e);      // 判断该属性为null的情况      if (null == fieldValue) {        throw new Exception("请求字段:" + fieldName + "不能为空");      }      // 如果该属性为字符串,判断其为空或空格的情况      if ("java.lang.String".equals(typeName)) {        if (StringUtils.isBlank((String)fieldValue)) {          throw new Exception("请求字段:" + fieldName + "不能为空");        }      }    }  }

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

人生就是一次充满未知的旅行,在乎的是沿途的风景,

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

相关文章:

你感兴趣的文章:

标签云: