gpt4 book ai didi

c# - 当ThreadPool构造新线程而不是重用它们时?

转载 作者:行者123 更新时间:2023-12-03 13:19:57 25 4
gpt4 key购买 nike

我从Reed Copsey的以下答案中阅读了以下段落:

Will values in my ThreadStatic variables still be there when cycled via ThreadPool?

The thread pool (by design) keeps the threads alive between calls. This means that the ThreadStatic variables will persist between calls to QueueUserWorkItem.

This behavior is also something you should not count on. The ThreadPool will (eventually, at its discretion) release threads back and let them end, and construct new threads as needed.



在什么条件下,线程池最终会构造新线程,而不是重用它们?

最佳答案

AFAIK在“无证”条件下。

首先要考虑的是,至少有4种常用的CLR hosting providers(ASP.Net, IE, shell exesSQLCLR),每种都有自己的策略。例如,SQLCLR托管使用SQL Server自己的Thread and Task architecture,并将通过收缩池(所有类型的池,包括线程)来对OS signals of pressure作出 react 。

因此,为什么不仅仅假设线程总是被回收,而您将是正确的(即,不要在池拥有的线程上保持状态)。

关于c# - 当ThreadPool构造新线程而不是重用它们时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24244086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com