为什么 Quartz.NET 创建具有相同线程 id 的线程?
我已经设置 Quartz.NET 来运行计划作业。当我查看线程 ID 时,我感到很惊讶。它们只是以 10 线程间隔重复。
我的意思是,例如,如果执行我的作业的第一个线程的 ID 为 101,那么第十一个线程(在第十一个时间间隔运行相同的作业)具有相同的 ID,101!
看起来 Quartz.NET 使用的是 10 个线程池,但更令人惊奇的是:为什么线程具有相同的 ID?他们不应该在每次创建时都获得新的线程ID吗?
I have set up Quartz.NET to run a scheduled job. It is amazing when I look at the thread IDs. They just get repeated in 10-thread interval.
I mean, for instance, if the first thread that gets to execute my job has ID 101 then the eleventh thread (that runs the same job at the eleventh interval) has the same ID, 101!
It seems that Quartz.NET is using a pool of 10 threads, but more amazing is: Why do the threads have the same ID? Shouldn't they get new thread ID each time they are created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 Quartz.NET 中的默认调度程序是 DirectSchedulerFactory,它使用内部 ThreadPool 实现(简单线程池)。
这将设置固定数量的线程,并为作业重用相同的线程。这会阻止您为每个作业获取新的线程 ID,因为线程 != 作业。
This is because the default scheduler in Quartz.NET is the DirectSchedulerFactory, which uses an internal ThreadPool implementation (SimpleThreadPool).
This will setup a fixed number of threads, and reuse the same threads for jobs. This prevents you from getting new thread IDs per job, since threads != jobs.
你还没有回答你自己的问题吗?我对quartz知之甚少,但如果它使用线程池,那么,是的,它将重用线程。旋转线程的高成本是线程池解决的问题之一,因此通过重用现有线程可以避免这种成本(即每次处理工作请求时不会旋转新线程)
Haven't you answered your own question? I know little about quartz, but if it uses a thread pool then, yes, it's going to reuse threads. The high cost of spinning up threads is one of the problems solved by a thread pool, so this cost is avoided by reusing existing threads (i.e. a new thread is not spun-up every time a work request is processed)