Giter VIP home page Giter VIP logo

timingwheelscheduler's Introduction

TimingWheelScheduler

a scheduler based on multi-level timing wheels. 一个基于多级时间轮的调度器。

如何使用

引用包

<ItemGroup>
  <PackageReference Include="TimingWheelScheduler" Version="*-*" />
</ItemGroup>

创建 scheduler

TimeSpan TickSpan;  //每次tick的时间长度
int TicksPerWheel;  //每层时间轮的tick数量(每层时间轮的大小)
int NumberOfWheels; //时间轮的总层数
var options = new TimingWheelSchedulerOptions(TickSpan, TicksPerWheel, NumberOfWheels)
{
    TaskScheduler = null; //TimingWheelScheduler 内部创建tick任务时所使用的 TaskScheduler,不指定时使用 TaskScheduler.Default
};

//创建manager,manager用于管理scheduler
var schedulerManager = TimingWheelScheduler.Create(options);
//获取调度器
var scheduler = schedulerManager.Scheduler;

//销毁调度器
//schedulerManager.Dispose();

使用默认的共享 scheduler

//TickSpan = 50ms
//TicksPerWheel = 255
//NumberOfWheels = 4 
var scheduler = TimingWheelScheduler.Shared;

创建定时任务

var delay = TimeSpan.FromSeconds(1);    //延时1s执行
var callback = () => Console.WriteLine("callbak");  //执行时的回调
var options = new ScheduleOptions() //调度选项,可选
{
    TaskScheduler = null,   //执行callback时使用的 TaskScheduler,不指定时使用 TaskScheduler.Default
};

//计划调度
var disposable = scheduler.Schedule(delay, callback, options);

//取消执行
//disposable.Dispose();

Async Delay

using var cts = new CancellationTokenSource();
var cancellationToken = cts.Token;  //取消token,可选
var delay = TimeSpan.FromSeconds(1);    //延时1s

//延时
await scheduler.Delay(delay, cancellationToken);

timingwheelscheduler's People

Contributors

stratosblue avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.