高效定时器算法

编程入门 行业动态 更新时间:2024-10-13 08:22:04
本文介绍了高效定时器算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实现简单计时器库的最佳算法是什么?该库应允许以下内容:

  • 启动计时器
  • 停止计时器
  • 要检查计时器是否仍在运行
  • 在计时器到期时,将调用回调函数

    计时器模块将允许计时器具有Ns的时间分辨率,并且每Ns将给模块一个踢,以提示模块检查过期的计时器。 / p>

    许多计时器可能同时处于活动状态。

    最好的算法需要满足以下目标

  • 在处理计时器到期回调时要坚决地启动/停止计时器
  • 允许计时器开始,停止和停止快速检查
  • 内存占用较小
  • 问候

    解决方案

    我看到的关于计时器的最佳算法是研究论文哈希和分层定时轮:用于高效实现计时器功能的数据结构

    我知道在Java中有Netty,JBoss的实现,而且我敢肯定,如果您使用Java编写,也可以在其他地方使用。

    What is the best algorithm to implement a simple timer library. The library should allow the following:

  • Timers to be started
  • Timers to be stopped
  • Timers to be checked whether they are still running
  • On Timer expiry a callback function will be called.

    The timer module will allow timers to have a time resolution of Ns and the module shall be given a kick every Ns to prompt the module to check for expired timers.

    Many timers may be simultaneously active.

    The best algorithm needs to meet the following goals

  • Be Robust to timers being started / stopped while processing a timer expiry callback
  • Allow timers to be started, stopped and checked quickly
  • Have a small memory footprint
  • Regards

    解决方案

    Best algorithm I have seen for timers is a timer wheel found in the research paper Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility

    I know in Java there is an implementation with Netty, JBoss and I am sure elsewhere too that you can use, if you are writing in Java.

    更多推荐

    高效定时器算法

    本文发布于:2023-11-29 21:17:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1647652.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:高效   定时器   算法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!