Timer 源码解读 (2) TaskQueue 最小堆实现的优先队列
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 necessaryif (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 leakfixDown(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 kidif (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)break;TimerTask tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;k = j;}}
Timer 源码解读 (2) TaskQueue 最小堆实现的优先队列相关推荐
- Java Review - PriorityQueue源码解读
文章目录 Pre PriorityQueue 概述 PriorityQueue 继承关系 PriorityQueue通过用数组表示的小顶堆实现 时间复杂度 构造函数 方法 add()和offer() ...
- PostgreSQL 源码解读(147)- Storage Manager#3(fsm_search函数)
本节简单介绍了PostgreSQL在执行插入过程中与存储相关的函数RecordAndGetPageWithFreeSpace->fsm_search,该函数搜索FSM,找到有足够空闲空间(min ...
- Bert系列(三)——源码解读之Pre-train
https://www.jianshu.com/p/22e462f01d8c pre-train是迁移学习的基础,虽然Google已经发布了各种预训练好的模型,而且因为资源消耗巨大,自己再预训练也不现 ...
- 【Android 异步操作】Timer 定时器 ( Timer 与 TimerTask 基本使用 | Timer 定时器常用用法 | Timer 源码分析 )
文章目录 一.Timer 定时器基本使用 二.Timer 定时器常用用法 三.Timer 源码分析 四.Timer 部分源码注释 五.源码及资源下载 参考文档 : Timer 定时器 API 文档 T ...
- Java Review - HashMap HashSet 源码解读
文章目录 概述 HashMap结构图 构造函数 重点方法源码解读 (1.7) put() get() remove() 1.8版本 HashMap put resize() 扩容 get HashSe ...
- Java Review - Queue和Stack 源码解读
文章目录 Pre 概述 Queue Deque ArrayDeque 一览 构造函数 属性 方法 addFirst() addLast() pollFirst() pollLast() peekFir ...
- Ubuntu 16.04下Caffe-SSD的应用(四)——ssd_pascal.py源码解读
前言 caffe-ssd所有的训练时的参数,全部由ssd_pascal.py来定义,之后再去调用相关的脚本和函数,所以想要训练自己的数据,首先要明白ssd_pascal.py各个定义参数的大体意思. ...
- KClient——kafka消息中间件源码解读
目录 kclient消息中间件 kclient-processor top.ninwoo.kclient.app.KClientApplication top.ninwoo.kclient.app.K ...
- 源码解读_Go Map源码解读之Map迭代
点击上方蓝色"后端开发杂谈"关注我们, 专注于后端日常开发技术分享 map 迭代 本文主要是针对map迭代部分的源码分析, 可能篇幅有些过长,且全部是代码, 请耐心阅读. 源码位置 ...
最新文章
- SQL Server 大数据量插入和索引关系
- Ubunt中卸载protobuf与安装3.6.0版本步骤
- Magento 显示下拉货币 How to add Currency selector to Magento’s header
- c语言case后接printf,C编程中switch。case 问题
- 跟着李开复去硅谷,你有疑惑我帮问 | 互动一则
- VBA 合并同文件夹下多工作簿中同名工作表到 一工作簿一工作表
- Windows禁用签名启动
- Remix-IDE(一)
- Flixel Dame 坦克大战(一)地图
- 关于AP3211KTR-G1
- 如何用VBA制作工资条
- 实验记录 | 为什么mtDNA的fastq数据会比对到常染色体上?
- PAT-A1008(C/C++代码解析)
- git原理学习记录:从基本指令到背后原理,实现一个简单的git
- 树莓派3 Android Pie 编译适配
- frl啥意思_frlday是什么意思
- seq2seq 解释
- (四)Android中的TextView组件
- ssm码农论坛毕业设计源码231126
- 管中窥豹——应试教育与一流科学人才究竟有多远的差距