延时任务执行,定时结束,定时终止,延时for循环

延时任务执行,定时结束,定时终止

  List<Object> List = xxxxMapper.selectByExample(example);
  long startTime = System.currentTimeMillis();
  ConcurrentHashMap<String, ScheduledFuture> taskMap = new ConcurrentHashMap<>(1);
        ScheduledExecutorService scheduleExecutor = new ScheduledThreadPoolExecutor(1);
        ScheduledFuture scheduledFuture = scheduleExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    if (getReport(recordList)) {
                        //通过cancel方式终止任务
                        logger.info("延时执行终止");
                        taskMap.get("scheduledFuture").cancel(true);
                        scheduleExecutor.shutdown();
                    }
                } catch (Exception e) {
                    logger.error("延时执行批量导入任务异常:"+e);
                }
            }
        }, 0, 30, TimeUnit.SECONDS);
        // 0表示首次执行任务的延迟时间,30表示每次执行任务的间隔时间,TimeUnit.SECONDS 秒
        taskMap.put("scheduledFuture", scheduledFuture);
         long endTime = System.currentTimeMillis();
         logger.info("时长 :"+(endTime-startTime));

  private boolean getReport(List<Object> List) throws IOException {
        //创建临时批量集合
        List<Object> tmpList=new ArrayList<Object>();
        for (int i = 0; i < 10; i++) {
            //向临时集合中添加数据
            if (recordList.size() > 0) {
                tmpList.add(recordList.get(0));
                recordList.remove(0);
            }
        }
        //执行批量数据查询报告并保存
        for (Object  data: tmpList) {
            Map<String, String> params = new HashMap<>();
            JSONObject resJson;
            try {
               //业务逻辑
            } catch (Exception e) 
                logger.error("败");
                continue;
            }
        }
        logger.info("批量:"+tmpList.size());
        // 原始集合数据全部执行完成返回true 终止定时,否则继续
        return recordList.size() == 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值