Timer Efficiency

前端 未结 4 746
臣服心动
臣服心动 2021-02-06 12:29

I am planning to develop a system with tens of thousands of objects in it, which will each have up to 42(but more likely down around 4 or 5) separate actions they will potential

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 13:01

    None of the above. The standard solution is to keep a list of events, such that each one points to the next one to occur. You then use a single timer and have it wake up only in time for the next event.

    edit

    Looks like this is called a timer wheel.

    edit

    As Sentinel pointed out, events should be dispatched to a thread pool. The handler for these events should do a small bit of work as quickly as possible, and without blocking. If it needs to do I/O, it should fire off an async task and terminate. Otherwise, a thread pool would overflow.

    The .NET 4.0 Task class might be helpful here, particularly for its continuation methods.

提交回复
热议问题