Java技术总监面试常见问题及答案,Kafka中的时间轮算法讲解

本文介绍了Java技术总监面试中常见的Kafka时间轮算法,探讨了添加延时任务的不同情况,包括时间到期、未到期但小于间隔以及未到期且大于间隔的处理方式。详细解释了时间轮的精度问题及其解决方案,以及如何通过多层时间轮实现长时间跨度的延迟任务调度。最后,文章提到了面试专题文档供读者参考。
摘要由CSDN通过智能技术生成

public class TimeWheel {



    /** 一个时间槽的时间 */

    private long tickMs;



    /** 时间轮大小 */

    private int wheelSize;



    /** 时间跨度 */

    private long interval;



    /** 槽 */

    private Bucket[] buckets;



    /** 时间轮指针 */

    private long currentTimestamp;



    /** 上层时间轮 */

    private volatile TimeWheel overflowWheel;



    public TimeWheel(long tickMs, int wheelSize, long currentTimestamp) {

        this.currentTimestamp = currentTimestamp;

        this.tickMs = tickMs;

        this.wheelSize = wheelSize;

        this.interval = tickMs * wheelSize;

        this.buckets = new Bucket[wheelSize];

        this.currentTimestamp = currentTimestamp - (currentTimestamp % tickMs);



        for (int i = 0; i < wheelSize; i++) {

            buckets[i] = new Bucket();

        }

    }

} 

将任务添加到时间轮中十分简单,对于每个时间轮来说,比如说秒级时间轮,和分级时间轮,都有它自己的过期槽。也就是delayMs < tickMs的时候。</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值