Timer 源码解读 (2) TaskQueue 最小堆实现的优先队列

本文深入解析了Java中Timer类的任务调度机制,详细介绍了其内部使用的平衡二叉堆数据结构及其实现原理。通过具体代码展示了如何添加任务并维持堆的有序状态,以及如何移除堆顶元素并重新调整堆结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Timer中的定义:

private final TaskQueue queue = new TaskQueue();

定义了一个队列:

class TaskQueue {
    /**
     * Priority queue represented as a balanced binary heap: the two children
     * of queue[n] are queue[2*n] and queue[2*n+1].  The priority queue is
     * ordered on the nextExecutionTime field: The TimerTask with the lowest
     * nextExecutionTime is in queue[1] (assuming the queue is nonempty).  For
     * each node n in the heap, and each descendant of n, d,
     * n.nextExecutionTime <= d.nextExecutionTime.
     */
    private TimerTask[] queue = new TimerTask[128];


Taskqueue是对一个队列的封装,这个队列实现的一个数据结构 a balanced binary heap 平衡二叉堆 并且根据TimerTask中的nextExecutionTime进行排序,最小值在堆顶

添加元素并排序:  

  void add(TimerTask task) {
        // Grow backing store if necessary
        if (size + 1 == queue.length)
            queue = Arrays.copyOf(queue, 2*queue.length);


        queue[++size] = task;
        fixUp(size);
    }



获得最小元素并排序:

    void removeMin() {
        queue[1] = queue[size];
        queue[size--] = null;  // Drop extra reference to prevent memory leak
        fixDown(1);
    }


整体重排序:

    void heapify() {
        for (int i = size/2; i >= 1; i--)
            fixDown(i);
    }

fixUp和fixDown是堆排序方法:当插入元素,移除元素的时候都需要重新排序,以使得最小元素在堆顶

    private void fixUp(int k) {
        while (k > 1) {
            int j = k >> 1;
            if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }
    private void fixDown(int k) {
        int j;
        while ((j = k << 1) <= size && j > 0) {
            if (j < size &&
                queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                j++; // j indexes smallest kid
            if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值