源码下载 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. vue将数据绑定到属性中

    *必须使用[] <tr v-for="(p,index) in prodects"> @*v-bind:class="styleType(index)&quo ...

  2. Java中的阻塞队列-SynchronousQueue

    SynchronousQueue是一个不存储元素的阻塞队列.每一个put操作必须等待一个take操作,否则不能继续添加元素.SynchronousQueue可以看成是一个传球手,负责把生产者线程处理的 ...

  3. java使用jdbc连接数据库步骤

    确定连接的数据库类型<mysql,oracle,db2,moangdb,sqlservlet> 下载数据库的驱动(http://mvnrepository.org),并把jar包添加到项目 ...

  4. struts2分页实现

    1.定义一个page类,里面包括每一个的数量,当前页码,总记录数,等 2.将page对象传入dao中,dao输出分页查询好的数据.返回到action中,action放到request中,jsp从req ...

  5. node-sass 安装报错解决办法

    npm install安装node-sass时出现以下问题: Cannot download https://github.com/sass/node-sass/releases/download/v ...

  6. Unified Service Desk Overview

    As we implement CRM in enterprise, we usually integrate with many other information system such as E ...

  7. bootstrap-table 数据表格行内修改

    bootstrap-table 数据行内修改js中设置列的属性 editable : { type : 'text',//数据显示在文本框内 emptytext : "--",// ...

  8. 腾讯云“动态加速”与“CDN”的区别——浅谈对“动态加速”的理解(可能有误)

    CDN的劣势及“动态加速”产生背景 通常CDN对静态内容支持较好,若使用其加速动态内容,可能会导致异常(如导致无法登录).当然,可以将动态内容的在CDN节点上的缓存时间设置为0秒来解决.但这毕竟是用户 ...

  9. VUE在页面没加载完的时候会显示原代码的处理方法

    CSS: [v-cloak] { display: none; } HTML : <div v-cloak> {{ message }} </div> 其中 v-cloak官方 ...

  10. grep的使用

    http://www.eguidedog.net/linux-tutorial/05-grep.php grep apple fruitlist.txt:在fruitlist.txt中查找apple字 ...