![](/img/trans.png)
[英]Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?
[英]Does .Net reuse the same thread over different requests?
我有以下課程:
public static class ThreadStaticContainer
{
[ThreadStatic]
private static Dictionary<string, string> _valueDictionary;
public static Dictionary<string, string> ValueDictionary
{
get { return _valueDictionary ?? (_valueDictionary = new Dictionary<string, string>()); }
}
}
它在我的MVC Action中被調用如下:
public ActionResult About()
{
ThreadStaticContainer.ValueDictionary.Add("1","1");
return View();
}
我經常得到例外:
已添加具有相同鍵的項目。
這讓我感到驚訝,因為我認為每個請求都有一個新線程。 但它的表現好像有時請求會重用一個線程。
這是怎么回事?
線程很貴。
為了避免創建太多線程,ASP.Net處理來自ThreadPool的可重用線程的請求。
因此,你不能像那樣使用[ThreadStatic]
。
相反,您應該將東西存儲在HttpContext.Items
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.