Dubbo组件 — HashedWheelTimer源码分析

本文详细分析了Dubbo中HashedWheelTimer的实现原理,包括基本概念如Timer、Timeout和TimerTask,以及HashedWheelTimeout的类结构、remove()、expire()方法。此外,还探讨了HashedWheelBucket的addTimeout()、pollTimeout()和expireTimeouts(),以及HashedWheelTimer的newTimeout()、Worker的run()、waitForNextTick()、transferTimeoutsToBuckets()和processCancelledTasks()等核心功能。
摘要由CSDN通过智能技术生成

目录

1.基本概念

1.Timer

2.Timeout

3.TimerTask

2.HashedWheelTimeout

1.类结构分析

2.remove()

3.expire()

3.HashedWheelBucket

1.addTimeout()

3.pollTimeout()

4.expireTimeouts()

4.HashedWheelTimer

1.类结构分析

2.newTimeout()

3.Worker

1.run()

2.waitForNextTick()

3.transferTimeoutsToBuckets()

4.processCancelledTasks()


1.基本概念

1.Timer

Timer用来调度和管理定时任务,提供定时任务的添加、删除、停止、运行等方法

public interface Timer {

    Timeout newTimeout(TimerTask task, long delay, TimeUnit unit);

  
    Set<Timeout> stop();

   
    boolean isStop();
}

2.Timeout

Timeout持有TimerTask,用来处理定时任务

public interface Timeout {

    Timer timer(); // 绑定的定时器

    TimerTask task(); // 绑定的定时任务

    boolean isExpired(); // 判断过期

    boolean isCancelled(); // 判断取消

    boolean cancel(); // 进行取消
}

3.TimerTask

TimerTask就是一个定时任务

public interface TimerTask {

    void run(Timeout timeout) throws Exception;
}

2.HashedWheelTimeout

1.类结构分析

private static final class HashedWheelTimeout implements Timeout {
	
    // 当前Timeout状态 
	private static final int ST_INIT = 0; // 初始化
	private static final int ST_CANCELLED = 1; // 取消
	private static final int ST_EXPIRED = 2; // 过期
    
    // state成员变量的更新器
	private static final AtomicIntegerFieldUpdater<HashedWheelTimeout> STATE_UPDATER =
			AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimeout.class, "state");
	
    
	private final HashedWheelTimer timer; // 绑定的定时器
	private final TimerTask task; // 绑定的任务
	private final long deadline; // 这个Timeout处理任务的deadline

	@SuppressWarnings({"unused", "FieldMayBeFinal", "RedundantFieldInitialization"})
	private volatile int state = ST_INIT;


	long remainingRounds; // 剩余的圈数 当剩余圈数不大于0时 当前Timeout才能去执行任务

	// 维护双链表的指针
	HashedWheelTimeout next;
	HashedWheelTimeout prev;

	
	HashedWheelBucket bucket; // 用来保存当前Timeout的桶

	HashedWheelTimeout(HashedWheelTimer timer, TimerTask task, long deadline) {
		this.timer = timer;
		this.task = task;
		this.deadline = deadline;
	}
	
	...
	
}	

2.remove()

void remove() {
    // 将当前Timeout从保存它的桶中移除 具体看下面HashedWheelBucket分析
	HashedWheelBucket bucket = this.bucket;
	if (bucket != null) {
		bucket.remove(this);
	} else {
		timer.pendingTimeouts.decrementAndGet();
	}
}

3.expire()

public void expire() {
    // 通过UPDATER修改state为expired状态 修改失败直接return
	if (!compareAndSetState(ST_INIT, ST_EXPIRED)) {
		return;
	}

	try {
        // 执行任务 具体可以参考RetryTimerTask#run()
		task.run(this);
	} catch (Throwable t) {
		if (logger.isWarnEnabled()) {
			logger.warn("An exception was thrown by " + TimerTask.class.getSimpleName() + '.', t);
		}
	}
}

3.HashedWheelBucket

1.addTimeout()

注意啦!!Timeout一开始是放在队列中的,并不是在时间轮上,是在Tick触发时才将队列中的元素散列到时间轮底层的桶上

void addTimeout(HashedWheelTimeout timeout) {
	assert timeout.bucket == null;
    
    // 设置当前Timeout的bucket
	timeout.bucket = this; 
    
    // 双向链表维护
	if (head == null) {
		head = tail = timeout; 
	} else {
		tail.next = timeout;
		timeout.prev = tail;
		tail = timeout;
	}
}
public HashedWheelTimeout remove(
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值