自定义容器:批处理任务

需求

容器容量达到10则批处理,如果超过5s容量没有10,也进行批处理。

实现

/**
 * @Desc:
 * @Author: heling
 * @Date: 2021/5/6 14:49
 */
@Slf4j
public class BatchHandleList extends Vector<String> {

    private static volatile BatchHandleList list;

    //集合容量达到10则批处理
    private static int batchSize = 10;

    //达到5s即使容器容量小于10也批处理
    private static int timeout = 5;

    //上一次处理时间戳
    private static volatile long lastHandTsp;

    final ScheduledExecutorService executor;

    private BatchHandleList() {
        super();
        this.executor = Executors.newScheduledThreadPool(1, (r) -> {
            Thread t = new Thread(r);
            t.setName("定期检查容器线程");
            t.setDaemon(true);
            return t;
        });
        this.executor.scheduleWithFixedDelay(() -> {
            try {
                if (System.currentTimeMillis() - lastHandTsp >= timeout * 1000L) {
                    log.info("========达到时间阈值,开始批处理========");
                    int size = size();
                    for (int i = 0; i < size; i++) {
                        String v = super.remove(0);
                        log.info("处理{}", v);
                    }
                    lastHandTsp = System.currentTimeMillis();
                }
            } catch (Exception e) {

            }
        }, 100L, timeout * 1000L, TimeUnit.MILLISECONDS);
        lastHandTsp = System.currentTimeMillis();
    }

    public static BatchHandleList getInstance() {
        if (null == list) {
            synchronized (BatchHandleList.class) {
                if (null == list) {
                    list = new BatchHandleList();
                }
            }
        }
        return list;
    }

    @Override
    public synchronized boolean add(String str) {
        boolean success = super.add(str);
        if (size() >= batchSize) {
            log.info("========达到批次数量阈值,开始批处理========");
            for (int i = 0; i < batchSize; i++) {
                String v = super.remove(0);
                log.info("处理{}", v);
            }
            lastHandTsp = System.currentTimeMillis();
        }
        return success;
    }

}

测试

    public static void main(String[] args) throws InterruptedException {
        BatchHandleList list = BatchHandleList.getInstance();

        for (int i = 1; i <= 15; i++) {
            list.add(i + "");
        }

        TimeUnit.SECONDS.sleep(10L);
        for (int i = 1; i < 5; i++) {
            TimeUnit.SECONDS.sleep(2L);
            list.add(i + "");
        }

        TimeUnit.MINUTES.sleep(10L);
    }

结果输出:

15:47:52.080 [main] INFO com.cxs.crm.ext.BatchHandleList - ========达到批次数量阈值,开始批处理========
15:47:52.082 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理1
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理2
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理3
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理4
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理5
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理6
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理7
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理8
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理9
15:47:52.084 [main] INFO com.cxs.crm.ext.BatchHandleList - 处理10
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - ========达到时间阈值,开始批处理========
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理11
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理12
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理13
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理14
15:47:57.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理15
15:48:02.181 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - ========达到时间阈值,开始批处理========
15:48:07.183 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - ========达到时间阈值,开始批处理========
15:48:07.183 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理1
15:48:07.183 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理2
15:48:12.184 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - ========达到时间阈值,开始批处理========
15:48:12.184 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理3
15:48:12.184 [定期检查容器线程] INFO com.cxs.crm.ext.BatchHandleList - 处理4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值