源码下载 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)的更多相关文章

  1. .Net之时间轮算法(终极版)定时任务

    TimeWheelDemo 一个基于时间轮原理的定时器 对时间轮的理解 其实我是有一篇文章(.Net 之时间轮算法(终极版))针对时间轮的理论理解的,但是,我想,为啥我看完时间轮原理后,会采用这样的方 ...

  2. .Net 之时间轮算法(终极版)

    关于时间轮算法的起始 我也认真的看了时间轮算法相关,大致都是如下的一个图 个人认为的问题 大部分文章在解释这个为何用时间轮的时候都再说 假设我们现在有一个很大的数组,专门用于存放延时任务.它的精度达到 ...

  3. 时间轮算法(TimingWheel)是如何实现的?

    前言 我在2. SOFAJRaft源码分析-JRaft的定时任务调度器是怎么做的?这篇文章里已经讲解过时间轮算法在JRaft中是怎么应用的,但是我感觉我并没有讲解清楚这个东西,导致看了这篇文章依然和没 ...

  4. 时间轮算法在Netty和Kafka中的应用,为什么不用Timer、延时线程池?

    大家好,我是yes. 最近看 Kafka 看到了时间轮算法,记得以前看 Netty 也看到过这玩意,没太过关注.今天就来看看时间轮到底是什么东西. 为什么要用时间轮算法来实现延迟操作? 延时操作 Ja ...

  5. 延时任务-基于netty时间轮算法实现

    一.时间轮算法简介 为了大家能够理解下文中的代码,我们先来简单了解一下netty时间轮算法的核心原理 时间轮算法名副其实,时间轮就是一个环形的数据结构,类似于表盘,将时间轮分成多个bucket(比如: ...

  6. [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用

    [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 目录 [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 0x00 摘要 0x01 业务领域 1.1 应用场景 0x02 定 ...

  7. kafka时间轮的原理(一)

    概述 早就想写关于kafka时间轮的随笔了,奈何时间不够,技术感觉理解不到位,现在把我之前学习到的进行整理一下,以便于以后并不会忘却.kafka时间轮是一个时间延时调度的工具,学习它可以掌握更加灵活先 ...

  8. 记录——时间轮定时器(lua 实现)

    很长一段时间里,我错误的认识了定时器.无意中,我发现了“时间轮”这个名词,让我对定时器有了新的看法. 我错误的认为,定时器只需要一个 tick 队列,按指定的时间周期遍历队列,检查 tick 倒计时满 ...

  9. 经典多级时间轮定时器(C语言版)

    经典多级时间轮定时器(C语言版) 文章目录 经典多级时间轮定时器(C语言版) 1. 序言 2. 多级时间轮实现框架 2.1 多级时间轮对象 2.2 时间轮对象 2.3 定时任务对象 2.4 双向链表 ...

随机推荐

  1. MySQL(四)

    一.使用正则表达式查询 SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEX ...

  2. SublimeText插件vue Syntax Highlight : vue语法高亮

    参考:http://www.cnblogs.com/cosnyang/p/6290950.html Vue.js 的单文件组件(*.vue)在 sublime 编辑器中是不被识别的.若要想高亮显示,需 ...

  3. JS的函数参数传递为值传递

    function setAge(i) { alert(i);//24 i = 18; alert(i);//18,i的改变不会影响外面的age }; var age = 24; setAge(age) ...

  4. Windows到Ubuntu免密登陆

    Windows到Ubuntu免密登陆 首先检查C盘用户文件夹下是否有.ssh文件夹,同时检查该文件夹中是否有至少两个文件,一个是xxx_rsa和xxx_rsa.pub,一个是私钥文件一个是公钥文件. ...

  5. python类的反射

    反射 通过字符串映射或者修改程序运行时的状态.属性.方法, 有一下4个方法 小例子--根据用户输入调用方法: class Dog(object): def __init__(self,name): s ...

  6. seleniumCSS用法

    http://sauceio.com/index.php/2009/10/selenium-tip-of-the-week-start-improving-your-locators/ http:// ...

  7. PB导出规定格式DBF文件 dBase 3 格式 222个字段

    最近在做一个给卫计委做数据上报的数据接口,接口要求使用奇葩的dBase 3数据库存储上报数据,忙活了几天总算搞好了,使用开发工具为powerbuild 12,222个字段的上报数据表生成DBF文件,写 ...

  8. cesium 动态水面效果

    后续继续更新

  9. 安裝 PHP 時出現undefined reference to `libiconv_open’ 之類的錯誤訊息

    在安裝 PHP 到系統中時要是發生「undefined reference to `libiconv_open'」之類的錯誤訊息,那表示在「./configure 」沒抓好一些環境變數值.錯誤發生點在 ...

  10. 第21章 DMA—直接存储区访问—零死角玩转STM32-F429系列

    第21章     DMA—直接存储区访问 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...