gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java编写超时工具类实例讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

1、说明

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

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

2、实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
  * TimeoutUtil <br>
  *
  * @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 <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);
    }
  }
 
 
}

实例知识点扩展:

属性校验工具类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
    * 校验对象中的属性。如果属性为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编写超时工具类内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://www.py.cn/java/jichu/27134.html 。

最后此篇关于Java编写超时工具类实例讲解的文章就讲到这里了,如果你想了解更多关于Java编写超时工具类实例讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com