2012-09-26 72 views
1

我的問題是,有什麼辦法可以限制我們使用Parallel.For循環時的線程數量。Parallel.For的線程限制?

假設我的機器有I3處理器,並且一次可以運行4個進程。所以當我在我的機器上運行Parallel.For循環時,會有4個線程在後臺運行。

有什麼辦法來限制線程數?

+0

http://stackoverflow.com/questions/5512312/is-it-possible-to-limit-the-cores-for-parallel-foreach http://stackoverflow.com/questions/1114317/does-parallel-foreach-limits-the-number-of-active-threads – MarcD

回答

1

您可以使用ParallelOptionsMaxDegreeOfParallelism = 4

Parallel.ForEach(words, 
        new ParallelOptions { 
         MaxDegreeOfParallelism = 4 
         }, 
        s => DoAction(s));