如何可扩展性是System.Threading.Timer? [英] How scalable is System.Threading.Timer?

查看:178
本文介绍了如何可扩展性是System.Threading.Timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,将需要使用定时器 S,但可能很多人。如何可扩展性是 System.Threading.Timer 类?该文件只是说这是轻量级,但没有进一步解释。难道这些定时器被吸进一个单独的线程(或非常小的线程池),用于处理所有回调代表的定时器,或做每个定时器都有自己的线程?

I'm writing an app that will need to make use of Timers, but potentially very many of them. How scalable is the System.Threading.Timer class? The documentation merely say it's "lightweight", but doesn't explain further. Do these timers get sucked into a single thread (or very small threadpool) that processes all the callbacks on behalf of a Timer, or does each Timer have its own thread?

我想另一种方式来改写的问题是:如何为 System.Threading.Timer 实施

I guess another way to rephrase the question is: How is System.Threading.Timer implemented?

推荐答案

我说,这回应了很多问题:不要忘了,(管理)来源$ C ​​$ C到框架可用。您可以使用这个工具来得到这一切: HTTP://www.$c$cplex.com/NetMassDownloader

I say this in response to a lot of questions: Don't forget that the (managed) source code to the framework is available. You can use this tool to get it all: http://www.codeplex.com/NetMassDownloader

不幸的是,在这种特定的情况下,大量的执行处于纯code,所以你不要看它...

Unfortunately, in this specific case, a lot of the implementation is in native code, so you don't get to look at it...

他们肯定使用线程池,而不是一个线程,每个定时器,虽然。

They definitely use pool threads rather than a thread-per-timer, though.

要实现定时器的大集合(这是内核如何做它的内部,我会怀疑是间接怎么你的大定时器的收集结束)是保持列表按时间排序,直到-到期的标准方法 - 因此系统永远只能有担心检查下一个定时器,将要到期,而不是​​整个列表

The standard way to implement a big collection of timers (which is how the kernel does it internally, and I would suspect is indirectly how your big collection of Timers ends up) is to maintain the list sorted by time-until-expiry - so the system only ever has to worry about checking the next timer which is going to expire, not the whole list.

粗略地讲,这使O(log n)的启动定时器和O(1),用于处理正在运行的定时器。

Roughly, this gives O(log n) for starting a timer and O(1) for processing running timers.

编辑:刚才一直在寻找杰夫里氏的书。他说,它使用一个单独的线程所有Timer对象(的Threading.Timer),这个线程知道什么时候下一个定时器(即如上)的原因,并呼吁ThreadPool.QueueUserWorkItem的回调适当。这样做,如果你没有完成一个计时服务一个回调之前下一个是因为,你的回调将重新进入在另一个线程池的效果。因此,在总结我怀疑你会看到一个很大的问题,有很多的定时器,但你可能遭受的线程池耗尽,如果大量的人被射击的同一个和/或它们的回调正在缓慢运行。

Just been looking in Jeff Richter's book. He says (of Threading.Timer) that it uses a single thread for all Timer objects, this thread knows when the next timer (i.e. as above) is due and calls ThreadPool.QueueUserWorkItem for the callbacks as appropriate. This has the effect that if you don't finish servicing one callback on a timer before the next is due, that your callback will reenter on another pool thread. So in summary I doubt you'll see a big problem with having lots of timers, but you might suffer thread pool exhaustion if large numbers of them are firing at the same timer and/or their callbacks are slow-running.

这篇关于如何可扩展性是System.Threading.Timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆