当前位置: 首页 > news >正文

可抢占执行模型的同周期多定时器管理器设计

1 核心设计

using System;
using System.Collections.Concurrent;
using System.Timers;namespace PriorityTimerManager;public sealed class PriorityTimerManager : IDisposable
{public sealed class OperationContext{private OperationContext(string tag, int periodMs){Tag = tag;PeriodMs = periodMs;}public string Tag { get; } = "";public int PeriodMs { get; }public static OperationContext Create(string tag, int periodMs){if (string.IsNullOrEmpty(tag)){throw new ArgumentException("Tag cannot be null or empty.", nameof(tag));}if (periodMs <= 0){throw new ArgumentOutOfRangeException(nameof(periodMs), "Period must be greater than zero.");}return new OperationContext(tag, periodMs);}}private sealed class Candidate{private Candidate(int periodMs, int basePriority, int strength, DateTime now, string tag, Action action){PeriodMs = periodMs;BasePriority = basePriority;Strength = strength;Arrival = now;SourceTag = tag;Execute = action;}public int PeriodMs { get; }public int BasePriority { get; } // 数字越小优先级越高public int Strength { get; } // 数字越小优先级越高public DateTime Arrival { get; }public string SourceTag { get; } = "";public Action Execute { get; } = () => { };public static Candidate Create(int periodMs, int basePriority, int strength, DateTime now, string tag, Action action){if (string.IsNullOrEmpty(tag)){throw new ArgumentException("Tag cannot be null or empty.", nameof(tag));}if (periodMs <= 0){throw new ArgumentOutOfRangeException(nameof(periodMs), "Period must be greater than zero.");}if (basePriority < 0){throw new ArgumentOutOfRangeException(nameof(basePriority), "Base priority must be non-negative.");}if (strength < 0){throw new ArgumentOutOfRangeException(nameof(strength), "Strength must be non-negative.");}return new Candidate(periodMs, basePriority, strength, now, tag, action);}}private sealed class CycleState{public readonly object LockObj = new();public Candidate? CurrentWinner;public Timer ResetTimer;private CycleState(Timer resetTimer){ResetTimer = resetTimer;}public static CycleState Create(Timer resetTimer){if (resetTimer == null){throw new ArgumentNullException(nameof(resetTimer), "Timer cannot be null.");}return new CycleState(resetTimer);}}private readonly ConcurrentDictionary<int, CycleState> _cycleStates = new();/// <summary>/// 决定操作强度的策略函数(可外部替换)/// </summary>public Func<OperationContext, int> StrengthStrategy { get; set; } = ctx => 100; // 默认所有操作强度=100(最弱)/// <summary>/// 注册一个优先级定时器/// </summary>/// <param name="periodMs"></param>/// <param name="basePriority"></param>/// <param name="tag"></param>/// <param name="action"></param>/// <returns></returns>public Timer RegisterTimer(int periodMs, int basePriority, string tag, Action action){// 确保该周期有对应状态var cs = _cycleStates.GetOrAdd(periodMs, p =>{var resetTimer = new Timer(p);var state = CycleState.Create(resetTimer);resetTimer.AutoReset = true;resetTimer.Elapsed += (s, e) => ResetCycle(state, p);resetTimer.Start();return state;});// 用户任务定时器var t = new Timer(periodMs){AutoReset = true};t.Elapsed += (s, e) =>{var strength = StrengthStrategy(OperationContext.Create(tag, periodMs));var c = Candidate.Create(periodMs, basePriority, strength, DateTime.Now, tag, action);OnTimerFired(cs, c);};t.Start();return t;}private void OnTimerFired(CycleState cs, Candidate c){lock (cs.LockObj){if (cs.CurrentWinner == null){cs.CurrentWinner = c;Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 初次执行: {c.SourceTag}");c.Execute();return;}var cur = cs.CurrentWinner;bool shouldPreempt = (c.Strength < cur.Strength) ||(c.Strength == cur.Strength && c.BasePriority < cur.BasePriority) ||(c.Strength == cur.Strength && c.BasePriority == cur.BasePriority &&c.Arrival < cur.Arrival);if (shouldPreempt){Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 抢占: {c.SourceTag} 覆盖 {cur.SourceTag}");cs.CurrentWinner = c;c.Execute();}else{Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 忽略: {c.SourceTag} (被 {cur.SourceTag} 压制)");}}}private static void ResetCycle(CycleState state, int periodMs){lock (state.LockObj){state.CurrentWinner = null;Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] ===== 新周期开始 ({periodMs}ms) =====");}}public void Dispose(){foreach (var kv in _cycleStates){kv.Value.ResetTimer.Dispose();}}
}

2 使用

using System;namespace PriorityTimerManager;class Program
{static void Main(){using var manager = new PriorityTimerManager();// 配置强度策略manager.StrengthStrategy = ctx =>{return ctx.Tag switch{"写0" => 0,     // 最强"写1" => 5,"写2" => 10,"写X" => 50,_ => 100        // 默认最弱};};// 6 秒周期任务manager.RegisterTimer(6000, basePriority: 2, tag: "写1", () => Console.WriteLine("执行: 写 1"));manager.RegisterTimer(6000, basePriority: 1, tag: "写0", () => Console.WriteLine("执行: 写 0"));manager.RegisterTimer(6000, basePriority: 3, tag: "写2", () => Console.WriteLine("执行: 写 2"));// 10 秒周期任务manager.RegisterTimer(10000, basePriority: 2, tag: "写X", () => Console.WriteLine("执行: 写 X"));manager.RegisterTimer(10000, basePriority: 1, tag: "写0_10s", () => Console.WriteLine("执行: 写 0 (10s)"));Console.WriteLine("定时器运行中,按任意键退出...");Console.ReadKey();}
}
http://www.sczhlp.com/news/19624/

相关文章:

  • seo优化是怎么优化的搜索引擎优化期末考试答案
  • 邢台哪个公司做网站seo引擎
  • 建设企业官方网站企业登录深圳seo公司排名
  • 做书籍封皮的网站如何刷关键词指数
  • 如何搭建高访问量的网站app推广工作是做什么的
  • 网站建好了还需要什么维护厦门人才网最新招聘信息网
  • 营销型网站建设市场临沂做网络优化的公司
  • 营销型网站设计官网天津seo网络
  • 织梦网站怎么做404页面模板青岛网络seo公司
  • 赣州人才招聘网宁波seo在线优化公司
  • 手机网站制作架构比较好的网络优化公司
  • 无障碍网站建设方案佛山市人民政府门户网站
  • unity网络游戏开发网站优化推广公司
  • 漯河网站建设公司企业网站制作模板
  • 东台做网站的百度集团
  • 大港油田建设官方网站朋友圈广告代理商官网
  • 下列不属于网站建设规划揭阳seo推广公司
  • 微信二维码网站制作推广文章的注意事项
  • 网站空间黄冈网站推广优化找哪家
  • 已有网站域名 怎么做网站深圳网络营销推广公司
  • 网站制作团队市场营销平台
  • 汽配公司的网站要怎么做教育培训网站官网
  • 外贸企业独立建站深圳seo优化外包
  • 网站建设方案书0网络营销的产品策略
  • 线上做交互的网站sem是什么?
  • 网站要挂工商标识怎么做推广赚钱的微信小程序
  • 工体做网站的公司潍坊新闻头条最新消息
  • php设计网站建设竞价托管推广代运营
  • 怎样申请做p2p融资网站下载百度安装到桌面
  • 网件路由器登录网址香港seo公司