• java的定时轮询



    ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
    service.scheduleAtFixedRate(() -> {

    LocalDateTime start = LocalDateTime.now().withNano(0).minusSeconds(1);
    LocalDateTime end = LocalDateTime.now().withNano(0);
    List<String> strings=null;
    for (int index = 0; index < GET_RETRY_TIMES; index++) {

    try {

    事件处理

    } catch (Exception e) {
    System.out.println("shibai%d" + start + end);
    logger.error(String.format("获取信息失败 开始时间:[%s], 结束时间:[%s]", start.toString(), end.toString()));
    }
    }
    for (int index = 0; index < SET_RETRY_TIMES; index++) {
    try {

    queueService.orderMsetMessage(strings);
    break;

    } catch (Exception e) {
    System.out.println(start.toString()+end.toString());
    logger.error(String.format("写入数据失败 开始时间:[%s], 结束时间:[%s]", start.toString(), end.toString()));

    }
    }

    }, 0, 1, TimeUnit.SECONDS);

    在java中,Timer类主要用于定时性、周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下

    (1)schedule方法:“fixed-delay”;如果第一次执行时间被delay了,随后的执行时间  上一次 实际执行完成的时间点 进行计算
    (2)scheduleAtFixedRate方法:“fixed-rate”;如果第一次执行时间被delay了,随后的执行时间按照 上一次开始的时间点 进行计算,并且为了”catch up”会多次执行任务,TimerTask中的执行体需要考虑同步
  • 相关阅读:
    js中的日期控件My97 DatePicker
    list中慎用remove
    ehcache注解全面解析
    servlet
    SpringMVC注解@RequestMapping全面解析
    SpringMVC注解@RequestParam全面解析
    lucene全文检索
    jenkins和hudson
    Mysql与PostgreSql数据库学习笔记
    前端学习笔记
  • 原文地址:https://www.cnblogs.com/jie123/p/5450149.html
Copyright © 2020-2023  润新知