Cookie標識),是高效參數傳遞模式。Increment等),多線程環境下安全修改共享變量,本質等同lock但更輕量。Thread Pool Sample:
Queuing 10 items to Thread Pool
Queue to Thread Pool 0 ... 9
Waiting for Thread Pool to drain
2 0 : HashCount.Count==0, Thread.CurrentThread.GetHashCode()==2
4 1 : HashCount.Count==1, Thread.CurrentThread.GetHashCode()==4
...
Setting eventX
Thread Pool has been drained (Event fired)
Load across threads
11 1
10 1
9 1
8 1
7 1
6 1
4 2
2 2
觀察可見:
ManualResetEvent作為完成信號,主線程WaitOne阻塞直至計數器達標Set()。線程池通過預創建、複用線程,消除頻繁創建銷毀開銷;QueueUserWorkItem簡化異步調度;WaitCallback統一入口適配任意方法。掌握其本質,方能在高併發、短任務場景中發揮最大效能。