时间轮算法的定时器(Delphi)
源码下载 http://files.cnblogs.com/lwm8246/uTimeWheel.rar
D7,XE2 编译测试OK
//时间轮算法的定时器
//-- : QQ unit uTimeWheel; interface uses
Windows,Classes,SysUtils,SyncObjs; type
PTWItem=^TTWItem;
TTWItem=record
UserData:Pointer; //用户数据
Tag:Integer; //用户数据
Positin:Integer;
Interval:Integer;//定时时间(ms)
end; TTimeWheel=class(TThread)
private
FCS:TCriticalSection;
FInterval:Integer;
FWorkDone:Boolean;
function getPosition: Integer;
protected
FPosition:Integer;
FSize:DWORD;
FList:TList;
procedure Execute();override;
procedure OnTime(PI:PTWItem);virtual;abstract;
public
//AOnTimeCount 最大触发次数
constructor Create(AOnTimeCount:DWORD;AInterval:Integer);virtual;
destructor Destroy();override;
procedure Lock();
procedure UnLock();
procedure Start();
procedure Stop();
//\\
function RegisterTime(AInterval:Integer):PTWItem;virtual;
public
property List:TList read FList;
property Position:Integer read getPosition;
property Interval:Integer read FInterval; //单位 ms
property WorkDone:Boolean read FWorkDone;
end; implementation { TTimeWheel } constructor TTimeWheel.Create(AOnTimeCount:DWORD;AInterval:Integer);
var
Index:DWORD;
PI:PTWItem;
begin
inherited Create(TRUE);
FCS := TCriticalSection.Create();
FLIst := TList.Create();
FList.Capacity := AOnTimeCount;
FSize := AOnTimeCount;
FInterval := AInterval;
for Index := to FSize - do
begin
New(PI);
PI^.UserData := nil;
PI^.Interval := ;
FList.Add(PI);
end;
FPosition := ;
FreeOnTerminate := FALSE;
FWorkDone := FALSE;
end; destructor TTimeWheel.Destroy;
var
Index:integer;
PI:PTWItem;
begin
FCS.Free();
for Index := to FList.Count - do
begin
PI := PTWItem(FList.Items[Index]);
if PI <> nil then Dispose(PI);
end;
FList.Free();
inherited;
end; procedure TTimeWheel.Execute;
procedure Delay(Value:Integer);
begin
while((not Terminated) and (Value > )) do
begin
Dec(Value,);
Sleep();
end;
end;
var
PI:PTWItem;
begin
while(not Terminated) do
begin
//Sleep(FInterval);
Delay(FInterval);
Lock();
try
Inc(FPosition);
FPosition := FPosition mod FSize;
PI := FList.Items[FPosition];
PI^.Positin := FPosition;
finally
UnLock();
end;
//触发时间到事件
if not Terminated then
begin
OnTime(PI);
end;
end;
FWorkDone := TRUE;
end; function TTimeWheel.getPosition: Integer;
begin
FCS.Enter();
Result := FPosition;
FCS.Leave();
end; procedure TTimeWheel.Lock;
begin
FCS.Enter();
end; function TTimeWheel.RegisterTime(AInterval: Integer): PTWItem;
var
Lfactor:Integer;
LPosition:Integer;
begin
if AInterVal > FInterval * FSize then
raise exception.CreateFmt('TTimeWheel.RegisterTime(%d),Out of Time Range',[AInterval]);
Lfactor := AInterval div FInterval;
LPosition := Position + Lfactor;
LPosition := LPosition mod FSize;
Result := FList.Items[LPosition];
Result^.Interval := AInterval;
Result^.Positin := LPosition;
end; procedure TTimeWheel.Start();
begin
Resume();
end; procedure TTimeWheel.Stop;
begin
Terminate();
while(TRUE) do
begin
if FWorkDone then Break;
Sleep();
end;
end; procedure TTimeWheel.UnLock;
begin
FCS.Leave();
end; end.
时间轮算法的定时器(Delphi)的更多相关文章
- .Net之时间轮算法(终极版)定时任务
TimeWheelDemo 一个基于时间轮原理的定时器 对时间轮的理解 其实我是有一篇文章(.Net 之时间轮算法(终极版))针对时间轮的理论理解的,但是,我想,为啥我看完时间轮原理后,会采用这样的方 ...
- .Net 之时间轮算法(终极版)
关于时间轮算法的起始 我也认真的看了时间轮算法相关,大致都是如下的一个图 个人认为的问题 大部分文章在解释这个为何用时间轮的时候都再说 假设我们现在有一个很大的数组,专门用于存放延时任务.它的精度达到 ...
- 时间轮算法(TimingWheel)是如何实现的?
前言 我在2. SOFAJRaft源码分析-JRaft的定时任务调度器是怎么做的?这篇文章里已经讲解过时间轮算法在JRaft中是怎么应用的,但是我感觉我并没有讲解清楚这个东西,导致看了这篇文章依然和没 ...
- 时间轮算法在Netty和Kafka中的应用,为什么不用Timer、延时线程池?
大家好,我是yes. 最近看 Kafka 看到了时间轮算法,记得以前看 Netty 也看到过这玩意,没太过关注.今天就来看看时间轮到底是什么东西. 为什么要用时间轮算法来实现延迟操作? 延时操作 Ja ...
- 延时任务-基于netty时间轮算法实现
一.时间轮算法简介 为了大家能够理解下文中的代码,我们先来简单了解一下netty时间轮算法的核心原理 时间轮算法名副其实,时间轮就是一个环形的数据结构,类似于表盘,将时间轮分成多个bucket(比如: ...
- [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用
[从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 目录 [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 0x00 摘要 0x01 业务领域 1.1 应用场景 0x02 定 ...
- kafka时间轮的原理(一)
概述 早就想写关于kafka时间轮的随笔了,奈何时间不够,技术感觉理解不到位,现在把我之前学习到的进行整理一下,以便于以后并不会忘却.kafka时间轮是一个时间延时调度的工具,学习它可以掌握更加灵活先 ...
- 记录——时间轮定时器(lua 实现)
很长一段时间里,我错误的认识了定时器.无意中,我发现了“时间轮”这个名词,让我对定时器有了新的看法. 我错误的认为,定时器只需要一个 tick 队列,按指定的时间周期遍历队列,检查 tick 倒计时满 ...
- 经典多级时间轮定时器(C语言版)
经典多级时间轮定时器(C语言版) 文章目录 经典多级时间轮定时器(C语言版) 1. 序言 2. 多级时间轮实现框架 2.1 多级时间轮对象 2.2 时间轮对象 2.3 定时任务对象 2.4 双向链表 ...
随机推荐
- [SVN]TortoiseSVN工具培训3─使用基本流程和图标说明
1.SVN的使用基本流程 注意:对于文件编辑方面,上图的编辑副本操作前建议进行Get lock操作,以防出现后续的冲突等异常报错. 2.SVN的基本图标说明
- myVision云服务商业数据分析解决方案
类型: 定制服务 软件包: business intelligence internet retailing solution collateral 联系服务商 产品详情 解决方案 概要 2014年, ...
- hermite 相关算法整理
设f(x)f(x)在节点a≤x0,x1,⋯,xn≤ba≤x0,x1,⋯,xn≤b处的函数值为f0,f1,...,fnf0,f1,...,fn,设P(x)为f(x)P(x)为f(x)在区间[a,b][a ...
- java中将数组、对象、Map、List转换成JSON数据
如果要将数组.对象.Map.List转换成JSON数据,那我们需要一些jar包: json-lib-2.4-jdk15.jar ezmorph-1.0.6.jar commons-logging.ja ...
- C盘空间太大,分区助手减小分区大小教程
首先看一个需要缩小C盘或需要减少分区空间的一个例子:“我的电脑里C盘剩余空间为530GB,除了C盘外还有一个D盘,但D盘的空间不到30GB,另外还有两个隐藏分区,一个200MB,一个15GB.我想把C ...
- Sublime Text3 + Markdown + 实时预览
Sublime Text3是一款给力的文本编辑器,通过安装插件可以编辑Markdown文本,在编辑Markdown文本的同时可以实时预览编辑效果. 安装准备: 找到菜单栏:Preferences → ...
- IOS 封装View的fram(X Y W H )
@interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) ...
- APP专项测试使用到的工具
最近在读<大话APP测试>,我也就是把需要使用的测试点做一个总结,目前是使用的工具进行的整理,后期慢慢把工具使用案例贴出来
- UESTC 1246 拆x3
用归纳法分析可以知道死循环只有4. 分析一下复杂度,如果n很大并且不是素数,根据基本不等式可以知道 sum factor(n) ≥ 2+n/2 ≍ n/2. 复杂度是O(T*logN*sqrt(N)) ...
- Gym 100090M Jumping along the Hummocks
题意: 从 前往后跳,要么跳一步,跳到相邻的位置,要么跳到下一个数字相同的位置,求跳到最后的最少步数. dp,但是会tle,我用map优化了一下. #include <bits/stdc++.h ...