C#中通过LRU实现通用高效的超时连接探测

2020-01-05 09:59:13于海丽

代码并不复杂,相信不用过多解释也能看懂相关操作原理。

测试

既然功能已经实现,接下来就要对代码进行测试看运行效果。测试代码比较简单首先开启一个Timer定时执行Detection,另外开一个线程去调用Update方法


class Program
  {
    public class TestDetector : IDetector
    {
      public double ActiveTime { get; set; }
      public string Name { get; set; }
      public LinkedListNode<IDetector> DetectorNode { get; set; }
    }
    static void Main(string[] args)
    {
      LRUDetector lRUDetector = new LRUDetector();
      lRUDetector.Timeout = (items) =>
      {
        foreach (TestDetector item in items)
          Console.WriteLine($"{(item.Name)} timeout {lRUDetector.GetTime() - item.ActiveTime}ms");
      };
      System.Threading.Timer timer = null;
      timer = new System.Threading.Timer(o =>
      {
        timer.Change(-1, -1);
        lRUDetector.Detection(5000);
        timer.Change(5000, 5000);
      }, null, 5000, 5000);
      System.Threading.ThreadPool.QueueUserWorkItem(o =>
      {
        int i = 0;
        while (true)
        {
          System.Threading.Thread.Sleep(500);
          i++;
          TestDetector testDetector = new TestDetector();
          testDetector.Name = "my name is " + i;
          lRUDetector.Update(testDetector);
        }
      });
      Console.Read();
    }
  }

运行效果:

C#,LRU,超时,连接

以上所述是小编给大家介绍的C#中通过LRU实现通用高效的超时连接探测,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对ASPKU网站的支持!


注:相关教程知识阅读请移步到c#教程频道。