This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks | |
void Main() | |
{ | |
var options = new ParallelOptions() { | |
MaxDegreeOfParallelism = 2 | |
}; | |
Parallel.For(0, 10, options, i => ExecTaskSync(i)); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System.Threading.Tasks | |
var maxIterations = 10000; | |
var maxDOP = 10; | |
// Divide into groups. | |
var parallelGroups = Enumerable.Range(0, maxIterations) | |
.GroupBy(r => (r % maxDOP)); | |
var parallelTasks = parallelGroups.Select(groups => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System.Threading.Tasks | |
const int MAX_DEGREE_OF_PARALLELISM = 2; | |
static SemaphoreSlim _semaphore = new SemaphoreSlim(MAX_DEGREE_OF_PARALLELISM); | |
async void Main() | |
{ | |
var tasks = Enumerable.Range(0, 10) | |
.Select(i => ExecTask(i)) | |
.ToArray(); |