Thread Pool如何處理Requests
第一、web server的.Net Framework負責維護一個thread pool,在.Net Framework 4.5中的預設值,一個thread pool可以容納的thread的最大值是5000個。
第二、每當出現一個request,就會自thread pool中指派一個thread處理request。
第三、當一個thread將本身負責的request處理完畢後,就會被放回thread pool。
第四、當thread pool中所有的thread都已經被指派處理時,後續的request就會被存放在queue中。
第五、倘若queue中的存放空間已滿,web server就會拒絕處理後續的request,並且出現錯誤代碼HTTP 503 status(Server Too Busy)。
第六、前述第五點的問題稱之為thread starvation。
第七、面對thread starvation,導致thread injection因為受限而速度下滑,將會衍生應用程式回應遲緩。
第八、當每一個thread可以有1MB的stack memory,而5000個thread全部耗盡時,需要5GB的記憶體空間,但是若將同步程式設計改為非同步程式設計,只需要50個thread即可完成相同的工作。
補充資料:
thread injection[2]
Thread injection is a technique that is used to insert and run executable code within the address space of another process.
Address space[3]
In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity.
starvation[4]
Starvation occurs when one or more threads in your program are blocked from gaining access to a resource and, as a result, cannot make progress.
deadlock[4]
Deadlock, the ultimate form of starvation, occurs when two or more threads are waiting on a condition that cannot be satisfied. Deadlock most often occurs when two (or more) threads are each waiting for the other(s) to do something.
參考資料來源:
[1]How Requests Are Processed by the Thread Pool
http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4
[2]What is the purpose of the Thread Injection check option within SCSP policies
http://www.symantec.com/business/support/index?page=content&id=TECH163625
[3]Address space
http://en.wikipedia.org/wiki/Address_space
[4]Starvation and Deadlock
http://www.math.uni-hamburg.de/doc/java/tutorial/essential/threads/deadlock.html