採用 .Net Framework 內建的 ThreadPool 雖然也不錯用,但若真的實際用來開發時,就會發現有許多使用情境會導致程式碼過於複雜、不容易閱讀。最近發現 Smart Thread Pool 似乎蠻不錯用的,如果你的程式有多份工作需要同步執行,而又不考慮程式執行的先後順序時,是可以考慮使用 Smart Thread Pool 來開發多執行緒程式。
Smart Thread Pool 元件的特性有:
- The number of threads dynamically changes according to the workload on the threads in the pool.
- Work items can return a value.
- A work item can be cancelled if it hasn't been executed yet.
- The caller thread's context is used when the work item is executed (limited).
- Usage of minimum number of Win32 event handles, so the handle count of the application won't explode.
- The caller can wait for multiple or all the work items to complete.
- Work item can have a
PostExecute
callback, which is called as soon the work item is completed. - The state object, that accompanies the work item, can be disposed automatically.
- Work item exceptions are sent back to the caller.
- Work items have priority.
- Work items group.
- The caller can suspend the start of a thread pool and work items group.
- Threads have priority.
若要學習 Smart Thread Pool 要如何使用,可以參考這篇文章。
相關連結